selector嵌套shape

前端之家收集整理的这篇文章主要介绍了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" />
原文链接:https://www.f2er.com/xml/296251.html

猜你在找的XML相关文章