XML文件数减少的示例代码分享

发布时间:2022-05-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了XML文件数减少的示例代码分享脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在andROId开发中,做出漂亮的ui的应用,往往有数量庞大的XMl文件。比如,我们要给一个Button加上一个selector,如果背景不是图片,就得写三个xML文件,分别是:
edIT_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<Shape xmlns:android="http://schemas.android.COM/apk/res/android" >
 
    <corners android:radius="3dip" />
    <gradient
        android:angle="90"
        android:endColor="#ffffff"
        android:startColor="#000000"
        android:tyPE="linear" />
</shape>

edit_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <corners android:radius="5dip" />
    <gradient
        android:angle="0"
        android:endColor="#000000"
        android:startColor="#ffffff"
        android:type="linear" />
</shape>

selector_edit.xml

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

一个按钮的selector就得三个xml,这样算来,xml文件的数量想少都太难了,其实我们可以把这三个文件合并成一个,写到一起,这样就能很大程序上减少让人眼花缭乱xml文件数。如下:
selector_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:state_PRessed="true">
        <shape>
            <corners android:radius="3dip" />
            <gradient android:angle="90"
                      android:endColor="#ffffff"
                      android:startColor="#000000"
                      android:type="linear" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="5dip" />
 
            <gradient android:angle="0"
                      android:endColor="#000000"
                      android:startColor="#ffffff"
                      android:type="linear" />
        </shape>
    </item>
</selector>

使用的时候和上面完全一样。但是xml文件的数量减少很多。

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/selector_anotate_icon"
        android:text="@string/BTn_text" />

以上就是XML文件数减少的示例代码分享的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的XML文件数减少的示例代码分享全部内容,希望文章能够帮你解决XML文件数减少的示例代码分享所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:XML