三、Drawable
1、所有Drawable资源都作为一个单独的文件存储在res/drawable文件夹中,每一个文件都包含一个单一的Drawable资源,文件名就是它的资源标识符。。Drawable资源一般指的是图片资源,也可以是复杂的复合Drawable。最常用的复合Drawable就是selector和shape
2、selector主要是用来根据用户行为动态改变ListView或者Button控件的背景。selector用法如下
step1:在res/drawable文件夹下新建一个xml文件,root元素为selector(假设文件名是app_selector.xml)
step2:编辑app_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片 -->
<item android:drawable="@drawable/pic1"/>
<!-- 没有焦点时的背景图片 -->
<item android:drawable="@drawable/pic1" android:state_window_focused="false"/>
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:drawable="@drawable/pic2" android:state_focused="true" android:state_pressed="true"/>
<!-- 触摸模式下单击时的背景图片 -->
<item android:drawable="@drawable/pic3" android:state_focused="false" android:state_pressed="true"/>
<!-- 选中时的图片背景 -->
<item android:drawable="@drawable/pic4" android:state_selected="true"/>
<!-- 获得焦点时的图片背景 -->
<item android:drawable="@drawable/pic5" android:state_focused="true"/>
<!-- 点击过后的图片背景 -->
<item android:state_checked="true" android:drawable="@drawable/pic6" />
<item android:state_checked="false" android:drawable="@drawable/pic57" />
</selector>
step3:引用selector资源
android:listSelector="@drawable/app_selector.xml"
或者,android:background="@drawable/app_selector.xml"
或者,Drawable drawable = getResources().getDrawable(R.drawable.app_selector.xml); listView.setSelector(drawable);
<!-- Android中常常使用shape来定义控件的一些显示属性 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 描边 -->
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<!-- 渐变 -->
<gradient
android:angle="225"
android:endColor="#DD2ECCFA"
android:startColor="#DD000000" />
<!-- 圆角 -->
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
{
View v = inflater.inflate(R.layout.app_fragment, null);
return v;
}