在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" />

更多相关文章

  1. 详细介绍Android 解析XML文件和生成XML文件的示例代码
  2. 具体介绍使用XmlReader读取xml文件的代码案例
  3. xml,文件操作功能类的示例代码详解
  4. XML布局文件的代码案例分享
  5. 详解读写XML文件的代码案例
  6. XML开发基础-查看XML文件
  7. xml文件正确性验证类的示例代码分析
  8. 详解xmlhttp组件获取远程文件并筛选出目标数据
  9. xml文件如何编辑的详情介绍

随机推荐

  1. 人工智能交互集成在线语音合成能力的Tips
  2. Android Building System
  3. Android扫盲篇
  4. ContentProvider
  5. Android M新控件之FloatingActionButton,T
  6. 【Android Dev Guide - 01】 - What Is A
  7. Android存在大Bug,导致误发短信
  8. android socket通信
  9. Android应用程序消息处理机制(Looper、Han
  10. Android基本操作1(意图使用、界面转换、按