31

我正在尝试使用状态按下和选择来制作一个按钮,我已经对选项卡做了同样的事情并且它可以工作,但我不知道为什么这里不起作用。我已经这样做了:

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>

在这里,我将可绘制对象称为背景:

 <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>

谢谢!!!!

4

2 回答 2

57

选择器中的第一项仅在按下并选择按钮时使用。如果您想在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 是否对按钮有意义。

于 2012-11-18T19:49:48.153 回答
12

可以使用形状内联项。

   <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>
于 2016-09-20T05:54:07.333 回答