StateListDrawable、ColorStateList
来源:互联网 发布:天猫漫踪和淘宝漫踪 编辑:程序博客网 时间:2024/05/13 06:30
StateListDrawable类介绍:
说明:该类定义了不同状态值下与之对应的图片资源,即我们可以利用该类保存多种状态值,多种图片资源。
常用方法有:public void addState(int[] stateSet,Drawable drawable)
功能说明:给特定的状态集合设置drawable图片资源。
使用方式:参考前面的hello_selection.xml文件,我们利用代码构建一个相同的StateListDrawable类对象。
//初始化一个空对象StateListDrawable stalistDrawable = new StateListDrawable();//获取对应的属性值 Android框架自带的属性 attrint 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(newint []{pressed , window_focused}, getResources().getDrawable(R.drawable.pic1));stalistDrawable.addState(newint []{pressed , -focused}, getResources().getDrawable(R.drawable.pic2);stalistDrawable.addState(newint []{selected }, getResources().getDrawable(R.drawable.pic3);stalistDrawable.addState(newint []{focused }, getResources().getDrawable(R.drawable.pic4);//没有任何状态时显示的图片,我们给它设置我空集合stalistDrawable.addState(newint []{}, getResources().getDrawable(R.drawable.pic5);
上面的“-”负号表示对应的属性值为false
当我们为某个View使用其作为背景色时,会根据状态进行背景图的转换。
public boolean isStateful()
功能:表明该状态改变了,对应的drawable图片是否会改变。
注:在StateListDrawable类中,该方法返回为true,显然状态改变后,我们的图片会跟着改变。
在Android应用的开发过程中,我们经常要根据控件的状态,改变控件的显示,比如EditText在focus或者是unfocus时设置背景颜色的不同,这个相信大家在以前肯定看过,通过Selector就可以实现,但是如何让EditText在focus或unfocus时设置不同的颜色呢》接下来就说说ColorStateList的功能。
新建一个项目,为了便于观察,在默认的main.xml文件里面拖入两个EditText,然后drawable文件夹新建一个color_stat_list.xml,在里面输入以下内容:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="#FF0000" android:state_focused="true" ></item> <item android:color="#00FF00" android:state_focused="false" ></item> </selector>
编辑EditText,设置他们的android:textColor=”@drawable/color_state_list”,形如:
<EditText android:id="@+id/editText1" android:textColor="@drawable/color_state_list" android:text="@string/app_name" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> <EditText android:id="@+id/editText1" android:textColor="@drawable/color_state_list" android:text="@string/app_name" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText>
感谢这位大神的铺路,转载出自:http://www.cnblogs.com/zhouchanwen
- StateListDrawable、ColorStateList
- StateListDrawable、ColorStateList
- [Android]代码实现ColorStateList及StateListDrawable
- [Android]代码实现ColorStateList及StateListDrawable
- [Android]代码实现ColorStateList及StateListDrawable
- [Android]代码实现ColorStateList及StateListDrawable
- [Android]代码实现ColorStateList及StateListDrawable
- [Android]代码实现ColorStateList及StateListDrawable
- Android入门——选择器selelctor之ColorStateList和StateListDrawable使用小结
- StateListDrawable
- StateListDrawable
- StateListDrawable
- ColorStateList用法
- ColorStateList用法
- ColorStateList资源
- ColorStateList资源
- ColorStateList资源
- android StateListDrawable
- Python中布尔类型 短路计算原理
- Java中注解--个人笔记
- Poj2104 K-th Number
- 13.线程面试题一
- 第五届蓝桥杯JavaB组第四题
- StateListDrawable、ColorStateList
- Qt-Image Viewer Example
- hihocoder 1483 区间价值
- CCF-CSP-2015-12-4 送货
- csdn Markdown编辑器使用方法模板
- Python的函数参数传递
- Cow Contest奶牛的比赛
- 【bzoj2731】[HNOI2012]三角形覆盖问题
- 使用Caffe进行手写数字识别执行流程解析