Androidbuttonselect并按下drawable

我试图做一个button与状态按和select,我已经做了与标签相同,它的作品,但我不知道为什么在这里不工作。 我已经这样做了:

button_sel.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@color/azulado" android:endColor="@color/azulBrillante" android:angle="270" /> <corners android:radius="@dimen/corner_radius" /> <stroke android:width="2px" android:color="@color/blanco" /> </shape> 

button_unsel.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="@color/botonesD" android:endColor="@color/botones" android:angle="270" /> <corners android:radius="@dimen/corner_radius" /> <stroke android:width="2px" android:color="@color/blanco" /> </shape> 

和select器,button.xml:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_sel" android:state_selected="true" android:state_pressed="true"/> <item android:drawable="@drawable/button_unsel" android:state_selected="false" android:state_pressed="false"/> </selector> 

在这里,我把drawable作为背景:

  <style name="button"> <item name="android:background">@drawable/button</item> <item name="android:textSize">@dimen/text_size</item> <item name="android:padding">@dimen/padding_button</item> <item name="android:textColor">@color/blanco</item> </style> 

谢谢!!!!

Solutions Collecting From Web of "Androidbuttonselect并按下drawable"

只有在按下并selectbutton时,才会使用select器中的第一项。 如果你想在你的button被按下或select时使用button_sel ,你应该这样做:

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

项目从上到下进行评估,最后一项是默认值。 虽然我不确定state_selected是否对button有意义。

可以使用形状内联项目。

  <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape....> </item> <item android:state_selected="true" > <shape....> </item> <item android:state_pressed="true" android:state_selected="true" > <shape...> </item> </selector> 

示例:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" > <shape android:shape="rectangle"> <gradient android:startColor="@color/md_amber_300" android:endColor="@color/md_amber_50" android:angle="270" /> <corners android:radius="@dimen/fab_margin" /> <stroke android:width="2px" android:color="@color/primaryColorDark_orange" /> </shape> </item> <item android:state_pressed="true" > <shape android:shape="rectangle"> <gradient android:startColor="@color/md_amber_300" android:endColor="@color/md_amber_50" android:angle="270" /> <corners android:radius="@dimen/fab_margin" /> <stroke android:width="2px" android:color="@color/primaryColorDark_orange" /> </shape> </item> <item android:state_pressed="true" android:state_selected="true" > <shape android:shape="rectangle"> <gradient android:startColor="@color/md_teal_500" android:endColor="@color/md_blue_400" android:angle="270" /> <corners android:radius="@dimen/fab_margin" /> <stroke android:width="2px" android:color="@color/md_amber_A400" /> </shape> </item> </selector>