减少XML文件数
发布时间:2020-12-16 02:06:15 所属栏目:百科 来源:网络整理
导读:在android开发中,做出漂亮的ui的应用,往往有数量庞大的xml文件。比如,我们要给一个Button加上一个selector,如果背景不是图片,就得写三个xml文件,分别是: edit_focused.xml ? xml version="1.0" encoding="utf-8" ? shape xmlns:android ="http://sche
在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文件数。如下: <?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" /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- JSON.parse()和JSON.stringify()
- c# – 自动实现装饰器方法
- JSTL常用标签小结之XML标签库
- reactjs – 在create-react-app中安装babel-plugin-styled-
- cocos creator打包编译报错: You have not accepted the li
- c# – 使用Asp.Net发送SendAsync电子邮件,但Page仍在等待?
- flash – 如何知道actionscript 1,actionscript 2还是actio
- Windows下MongoDB安装操作
- ajax跨域请求webservice webconfig配置
- PostgreSQL和Kingbase中设置search_path