我正在尝试按下状态按钮并选择, 我已经对标签做了同样的工作,但是我不知道为什么这里不起作用。 我这样做了:
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>
选择器,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>
谢谢!!!!
答案 0 :(得分:53)
仅在按下并选择按钮时才使用选择器中的第一项。如果您想在按下或选择按钮时使用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是否对按钮有意义。
答案 1 :(得分:7)
可以使用形状内联项目。
<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>