Android中的Drawable资源—— StateListDrawable
2016-06-14 14:56
369 查看
这种使用xml来定义图片的方式在buttion中经常使用,它会根据状态来显示不同的图片资源。
存放位置:
res/drawable/
使用方式:
在Java文件中:R.drawable.filename
例如:
在xml布局文件中:@[package:]drawable/filename
例如:
语法:
例如:
另外,需要说的是,上面定义的这个xml会被相应的解析为StateListDrawable对象,所以我们也可以使用代码来进行实现。
上面的“-”负号表示对应的属性值为false
https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://blog.csdn.net/qinjuning/article/details/7474827
存放位置:
res/drawable/
使用方式:
在Java文件中:R.drawable.filename
例如:
Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.myimage);
在xml布局文件中:@[package:]drawable/filename
例如:
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/myninepatch" />
语法:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
例如:
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 触摸时并且当前窗口处于交互状态 --> <item android:state_pressed="true" android:state_window_focused="true" android:drawable= "@drawable/pic1" /> <!-- 触摸时并且没有获得焦点状态 --> <item android:state_pressed="true" android:state_focused="false" android:drawable="@drawable/pic2" /> <!--选中时的图片背景--> <item android:state_selected="true" android:drawable="@drawable/pic3" /> <!--获得焦点时的图片背景--> <item android:state_focused="true" android:drawable="@drawable/pic4" /> <!-- 窗口没有处于交互时的背景图片 --> <item android:drawable="@drawable/pic5" /> </selector>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
另外,需要说的是,上面定义的这个xml会被相应的解析为StateListDrawable对象,所以我们也可以使用代码来进行实现。
//初始化一个空对象 StateListDrawable stalistDrawable = new StateListDrawable(); //获取对应的属性值 Android框架自带的属性 attr int pressed = android.R.attr.state_pressed; int window_focused = android.R.attr.state_window_focused; int focused = android.R.attr.state_focused; int selected = android.R.attr.state_selected; stalistDrawable.addState(new int []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1)); stalistDrawable.addState(new int []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2); stalistDrawable.addState(new int []{selected }, getResources().getDrawable(R.drawable.pic3); stalistDrawable.addState(new int []{focused }, getResources().getDrawable(R.drawable.pic4); //没有任何状态时显示的图片,我们给它设置我空集合 stalistDrawable.addState(new int []{}, getResources().getDrawable(R.drawable.pic5);
上面的“-”负号表示对应的属性值为false
https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://blog.csdn.net/qinjuning/article/details/7474827
相关文章推荐
- Android 开发之 手势开发
- Android NDK 编译 引用第三方 so 文件
- Android ListViewview入门
- edittext只能输入数字和小数点,且小数点后只能输入两位
- 初识Android Studio中的Gradle
- Android的manifest配置
- 反射
- Android下使用W25Q32
- 检测和解决Android应用的性能问题
- ScrollView冲突问题
- Android中bindService()启动Service的过程分析
- 常用Android代码
- ViewFlipper实现View轮播点击等效果
- Android开发fragment的使用
- android中src和background区别
- Android高手进阶教程(二十五)之---Android 中的AIDL!!!
- Android多线程编程之HandlerThread学习篇(四)
- Android布局优化之ViewStub、include、merge使用与源码分析
- Android学习笔记四之Activity
- Android绘图