selector嵌套shape

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/yellow"/>
            <stroke android:color="@color/white" android:width="@dimen/dp1"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/yellow"/>
            <stroke android:color="@color/white" android:width="@dimen/dp1"/>
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/yellow"/>
            <stroke android:color="@color/white" android:width="@dimen/dp1"/>
        </shape>
    </item>



    <item >
        <shape android:shape="rectangle">
            <solid android:color="@color/black"/>
            <stroke android:color="@color/white" android:width="@dimen/dp1"/>
        </shape>
    </item>
</selector>

其实可以将shape单独拎出来,单独出来,其效果反而更好,增加可复用性,降低ctrl+c和ctrl+v,而且只专注于一项事情,此篇幅只是说selector是可行的。

单独拎出来如下:
1、创建shape:
stroke_white_solic_yellow_rec_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/yellow"/>
    <stroke android:color="@color/white" android:width="@dimen/dp1"/>
</shape>

2、设置selector

 <item android:state_checked="true" android:drawable="@drawable/stroke_white_solic_yellow_rec_shape" />

你可能感兴趣的:(android,xml,button,selector,shape)