Android button select and press drawable

情到浓时终转凉″ 提交于 2019-11-29 23:54:12

The first item in your selector is only used, when the button is pressed AND selected. If you want to use button_sel when your button is pressed OR selected, you should do something like this:

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

The items are evaluated from top to bottom, the last one is the default. Though I am not sure if state_selected makes sense for buttons.

Can use shape inline item.

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

For sample :

<?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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!