ColorStateList
ColorStateList功能在XML中的使用
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="hex_color" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
在XML中我们可以通过不同状态设置true然后设置color="hex_color"来达到不同状态显示不同颜色的目的,下面是总结的状态表
通过以上的设置我们就可以使用Color state list resource
在代码里动态设置状态色
public ColorStateList (int[][] states, int[] colors)
以上是ColorStateList的构造方法。第一个参数是状态,也就是上述表格中的状态;第二个参数是颜色,是第一个参数二维数组中的第一维数组对应状态集合对应的颜色。听上去很绕口,看代码就可以轻易了解了
private ColorStateList createColorStateList(int selected,int normal) { int[] colors = new int[] { selected, normal}; int[][] states = new int[2][]; states[0] = new int[] { android.R.attr.state_selected , android.R.attr.state_enabled}; states[1] = new int[] { android.R.attr.state_enabled }; ColorStateList colorList = new ColorStateList(states, colors); return colorList; }
上述代码states[0]一维数组里有两个状态,当着两个状态都成立的时候selected这个颜色才生效,所以这里回答了一个问题,为什么第一个参数是二维的,因为可以对应多个状态。然后我们把颜色设置即可。
textView.setTextColor(createColorStateList(Color.parseColor("#333ccc"),R.color.color_333333));
这样就可以代码动态设置状态颜色了。
注意:这个是为view设置颜色,不是背景,如果为背景设置颜色,需要使用StateListDrawable。
StateListDrawable
StateListDrawable所支持的11种状态
<?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>
StateListDrawable的使用
xml静态使用
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:state_hovered="true" android:drawable="@drawable/button_focused" /> <!-- hovered --> <item android:drawable="@drawable/button_normal" /> <!-- default --> </selector>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" />
Java动态代码构造并使用
//初始化一个空对象 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(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);
设置item的selector
xml方式
直接在drawable文件夹中添加一个xml文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/icon_press" android:state_pressed="true"/> <item android:drawable="@drawable/icon_select" android:state_selected="true"/> <item android:drawable="@drawable/icon_default"/> </selector>
或者
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="#FFFFFF" android:state_pressed="true" /> <item android:drawable="#F3F3F3" /> </selector>
在drawable下创建的xml都是作为背景使用的,背景可以使用纯颜色也可以使用图片。
动态添加
StateListDrawable selector = new StateListDrawable(); Drawable pressDrawable = getResources().getDrawable(R.drawable.pressDrawableId); Drawable selectDrawable = getResources().getDrawable(R.drawable.selectDrawableId); Drawable defaultDrawable = getResources().getDrawable(R.drawable.defaultDrawableId); selector.addState(new int[]{android.R.attr.state_pressed}, pressDrawable); selector.addState(new int[]{android.R.attr.state_selected}, selectDrawable); // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效 selector.addState(new int[]{}, defaultDrawable);
然后将该select动态设置为控件背景。
public static StateListDrawable createStateListDrawable(String selectedColor, String normalColor) { StateListDrawable selector = new StateListDrawable(); Drawable pressDrawable = new ColorDrawable(Color.parseColor(selectedColor)); Drawable selectDrawable = new ColorDrawable(Color.parseColor(selectedColor)); Drawable defaultDrawable = new ColorDrawable(Color.parseColor(normalColor)); selector.addState(new int[]{android.R.attr.state_pressed}, pressDrawable); selector.addState(new int[]{android.R.attr.state_selected}, selectDrawable); // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效 selector.addState(new int[]{}, defaultDrawable); return selector; }
使用:
Drawable background = ViewUtil.createStateListDrawable("#99cc00", "#dddddd"); frameLayout.setBackground(background);
将color转drawable
// 使用 ColorDrawable 设置背景颜色 int color = Color.RED; ColorDrawable colorDrawable = new ColorDrawable(color); findViewById(R.id.my_view).setBackground(colorDrawable); // 使用 GradientDrawable 设置背景颜色 int color2 = Color.BLUE; GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(color2); findViewById(R.id.my_view2).setBackground(gradientDrawable);