有状态的drawbale中嵌套shape

来源:互联网 发布:数据库事务是什么意思 编辑:程序博客网 时间:2024/06/05 23:48

一直以来,定义有状态的drawable资源需要引用shape时,都是单独创建一个shape,再创建一个drawable,类似于这样的形式:

shape.xml,类似于这样

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

drawable.xml,类似于这样

<?xml version="1.0" encoding="utf-8"?>    <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/shape" android:state_pressed="true" /></selector>

偶然间看见一段代码,没有单独定义shape文件,而是将shape的内容嵌套在drawable中,类似于这样:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true">    <shape>        <solid android:color="@android:color/white" />    </shape></item></selector>

才发现,其实如果你的shape文件没有被复用,而且又想减少文件个数,让xml的作用更加明确的话,完全可以参考这种做法。

总而言之

1 drawable中selector的item可以嵌套shape,来减少文件数量;
2 如果shape有复用,建议还是将其单列为一个xml文件。

原创粉丝点击