Android的按钮选择,然后按绘制(Android button select and press

2019-07-03 22:25发布

我试图与状态按下一个按钮,然后选择,我已经做了与标签相同,它的工作原理,但我不知道为什么在这里是行不通的。 我已经做了这样的:

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>

谢谢!!!!

Answer 1:

在您选择的第一个项目仅用于,当被按下选择按钮。 如果你想使用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有意义的按钮。



Answer 2:

可以使用形状联项目。

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


文章来源: Android button select and press drawable