2017-03-29 8 views
1

drawableLeftアイコンを中央に配置できません。
テキストの左にアイコンを簡単に置くことができますが、重力を中央に設定すると、テキストのみが中央に表示されますが、アイコンは表示されません。テキスト付きの中央のDrawableLeft

<Button 
    android:id="@+id/patient_list_button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/patientList" 
    android:drawableLeft="@drawable/ic_icon_two_heads" 
    android:layout_marginBottom="15dp" 
    style="@style/darkBlueButtonWithImage"/> 

これは私が欲しいものです:
enter image description here
は、これは私が持っているものです。
enter image description here

<style name="darkBlueButtonWithImage"> 
    <item name="android:drawablePadding">10dp</item> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:paddingLeft">10dp</item> 
    <item name="android:background">@drawable/radius_dark_blue_button</item> 
    <item name="android:gravity">center_vertical|left</item> 
    <item name="android:drawableTint">@color/white</item> <!-- has to be set in activity.java --> 
</style> 

どのように私はこれを達成することができますか?

+0

drawable paddingを使用する –

+0

@JanardhanRパディングを使用すると、ボタンのテキストが動的であれば不可能になります。 –

+0

あなたのスタイルは 'center_vertical | left'に設定されています。それを 'center'に置き換えてみてください – Ricardo

答えて

1

Button属性android:drawableLeftandroid:drawablePaddingを使用すると、期待した結果が得られません。 RelativeLayoutまたはLinearLayout,TextViewおよびImageViewを使用してカスタムボタンを作成できます。 <selector>を使用して、ボタンの状態(normal/pressed)の動作を定義します。

ここでは動作例を示します。これを試してみてください:

<!--- Custom Button --> 
<RelativeLayout 
    android:id="@+id/patient_list_button" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:clickable="true" 
    android:background="@drawable/custom_button_selector"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_centerInParent="true" 
     android:gravity="center_vertical"> 

     <ImageView 
      android:layout_width="24dp" 
      android:layout_height="24dp" 
      android:src="@drawable/icon_refresh"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:textSize="16dp" 
      android:text="CUSTOM BUTTON" 
      android:textStyle="bold" 
      android:textColor="#FFFFFF"/> 

    </LinearLayout> 
</RelativeLayout> 

custom_button_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_pressed="true" 
     android:state_enabled="true" 
     android:drawable="@drawable/bg_custom_button_pressed" /> 
    <item 
     android:state_focused="true" 
     android:state_enabled="true" 
     android:drawable="@drawable/bg_custom_button_pressed" /> 
    <item 
     android:state_enabled="true" 
     android:drawable="@drawable/bg_custom_button_normal" /> 
</selector> 

bg_c​​ustom_button_normal.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <!-- view background color --> 
    <solid 
     android:color="#01A1DD" > 
    </solid> 

    <!-- Here is the corner radius --> 
    <corners android:radius="8dp" > 
    </corners> 

</shape> 

bg_c​​ustom_button_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <!-- view background color --> 
    <solid 
     android:color="#303F9F"> 
    </solid> 

    <!-- Here is the corner radius --> 
    <corners android:radius="8dp" > 
    </corners> 

</shape> 

OUTPUT:

enter image description here 希望これは〜

+0

このアイコンはどこにありますか? :)それは素晴らしい作品:) –

+0

@MarekCzaplicki私の喜び:)。 Androidスタジオの '' Image Asset ''オプションを使ってアイコンを取得することができます。あなたの任意のプロジェクト '' File> New> Image Asset ''を右クリックするだけです。ありがとう〜 – FAT

1

Googleのアプローチを9000回以上試してみた結果、ViewGroupを使用して両者を並べると、たいていRelativeLayoutまたはLinearLayoutになり、ImageViewとTextViewが追加されました。それは不自由ですが、drawableStart/Endには欠けている機能がたくさんあるので、あなたは「あなたができない」ことを理解する前に多くの時間を無駄にします。

アライメント、ティント、ベクタなど「ビルドイン」ドロウアブルでは、これらのすべてのことが困難または不可能です。

0

a)の "paddingLeft" の値を大きくして、あなたのケースで "drawablePadding" を縮小または削除、適切なものに値を調整するのに役立ちます;たとえば、

android:paddingLeft="50dp" 
android:gravity="center_vertical|left" 

b)カスタム表示を使用します。

関連する問題