在按钮选择器中更改drawableleft

时间:2014-12-03 08:57:48

标签: android

我发现这个Q& A Is it possible to change the left drawable of a Button in Selector xml?并不适合我。我试过这个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue_rounded_button" android:drawableLeft="@drawable/aj_share_button_drawableleft" android:state_pressed="true"/>
    <item android:drawable="@drawable/blue_rounded_button_frame" android:drawableLeft="@drawable/aj_share_button_drawableleft" />
</selector>


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/aj_share_button_white_icon" android:state_pressed="true"/>
    <item android:drawable="@drawable/aj_share_button_blue_icon" />
</selector>

像这样设置选择器:

<Button android:id="@+id/button1" 
android:background="@drawable/aj_share_button_state"
android:drawableLeft="@drawable/aj_share_button_drawableleft" /> 

有人可以告诉我如何使其有效吗?

1 个答案:

答案 0 :(得分:10)

我建议: 您的按钮xml:

<Button android:id="@+id/button1" 
    android:background="@drawable/selector_aj_share_button_state"
    android:drawableLeft="@drawable/selector_aj_share_button_drawableleft"
/> 

selector_aj_share_button_drawableleft.xml(在你的drawable文件夹中)

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

现在可以使用左侧绘图。尝试使用selector_aj_share_button_state做同样的事情。希望它有所帮助!