目录介绍
1.selector简单介绍
2.selector创建方式
3.在Xml中标签说明
4.状态设置说明
5.举例子,在按钮的背景中
6.举例子,用于按钮的文本颜色
1.selector简单介绍
2.selector创建方式
3.在Xml中标签说明
4.状态设置说明
5.举例子,在按钮的背景中
6.举例子,用于按钮的文本颜色
7.代码中动态设置shape
1.selector简单介绍
实际应用中,很多地方比如按钮Button、Tab、ListItem等都是不同状态有不同的展示形状。
举个例子,一个按钮的背景,默认时是一个形状,按下时是一个形状,不可操作时又是另一个形状。
有时候,不同状态下改变的不只是背景、图片等,文字颜色也会相应改变。而要处理这些不同状态下展示什么的问题,就要用selector来实现。
2.selector创建方式
第一种:在Xml中直接创建selector的Xml文件,容易掌握,简单但是不灵活
第二种:在代码中动态创建selector,较为复杂,但是灵活,应用场景是选择器状态是不断变化的
3.在Xml中标签说明
selector标签,可以添加一个或多个item子标签,而相应的状态是在item标签中定义的。
定义的xml文件可以作为两种资源使用:drawable和color。
1.作为drawable资源使用时,一般和shape一样放于drawable目录下,item必须指定android:drawable属性。
2.作为color资源使用时,则放于color目录下,item必须指定android:color属性。
4.状态设置说明
android:state_enabled **设置触摸或点击事件是否可用状态**,一般只在false时设置该属性,表示不可用状态
android:state_pressed **设置是否按压状态**,一般在true时设置该属性,表示已按压状态,默认为false
android:state_selected **设置是否选中状态**,true表示已选中,false表示未选中
android:state_checked: **设置是否勾选状态**,主要用于CheckBox和RadioButton,true表示已被勾选,false表示未被勾选
android:state_checkable **设置勾选是否可用状态**,类似state_enabled,只是state_enabled会影响触摸或点击事件,state_checkable影响勾选事件
android:state_focused **设置是否获得焦点状态**,true表示获得焦点,默认为false,表示未获得焦点
android:state_window_focused **设置当前窗口是否获得焦点状态**,true表示获得焦点,false表示未获得焦点,例如拉下通知栏或弹出对话框时, 当前界面就会失去焦点;另外,ListView的ListItem获得焦点时也会触发true状态,可以理解为当前窗口就是ListItem本身
android:state_activated **设置是否被激活状态**,true表示被激活,false表示未激活,API Level 11及以上才支持,可通过代码调用控件的setActivated(boolean)方法设置是否激活该控件
android:state_hovered **设置是否鼠标在上面滑动的状态**,true表示鼠标在上面滑动,默认为false,API Level 14及以上才支持
补充:selector标签下有两个比较有用的属性要说一下,添加了下面两个属性之后,则会在状态改变时出现淡入淡出效果,
但必须在API Level 11及以上才支持
android:exitFadeDuration **状态改变时,旧状态消失时的淡出时间,以毫秒为单位**
android:enterFadeDuration **状态改变时,新状态展示时的淡入时间,以毫秒为单位**
5.举例子,在按钮的背景中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 当前窗口失去焦点时 -->
<item android:drawable="@drawable/bg_btn_lost_window_focused" android:state_window_focused="false" />
<!-- 不可用时 -->
<item android:drawable="@drawable/bg_btn_disable" android:state_enabled="false" />
<!-- 按压时 -->
<item android:drawable="@drawable/bg_btn_pressed" android:state_pressed="true" />
<!-- 被选中时 -->
<item android:drawable="@drawable/bg_btn_selected" android:state_selected="true" />
<!-- 被激活时 -->
<item android:drawable="@drawable/bg_btn_activated" android:state_activated="true" />
<!-- 默认时 -->
<item android:drawable="@drawable/bg_btn_normal" />
</selector>
6.举例子,用于按钮的文本颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 当前窗口失去焦点时 -->
<item android:color="@android:color/black" android:state_window_focused="false" />
<!-- 不可用时 -->
<item android:color="@android:color/background_light" android:state_enabled="false" />
<!-- 按压时 -->
<item android:color="@android:color/holo_blue_light" android:state_pressed="true" />
<!-- 被选中时 -->
<item android:color="@android:color/holo_green_dark" android:state_selected="true" />
<!-- 被激活时 -->
<item android:color="@android:color/holo_green_light" android:state_activated="true" />
<!-- 默认时 -->
<item android:color="@android:color/white" />
7.代码中动态设置shape
1.设置背景选择器
public static Drawable createColorShapeSelector(int[] pressState, int[] color, int radius,Context mContext) {
StateListDrawable stateListDrawable = new StateListDrawable(); // 创建一个选择器对象
int[] normalState = {};
Drawable pressDrawable;
Drawable normalDrawable;
if(null==mContext){
// 创建一个按下状态和按下状态对应的图片
// int[] pressState = {android.R.attr.state_pressed, android.R.attr.state_enabled};
pressDrawable = createColorShape(color[0], radius);
// 创建一个正常状态和正常状态对应的图片
normalDrawable = createColorShape(color[1], radius);
}else {
Resources resources = mContext.getResources();
pressDrawable = resources.getDrawable(color[0]);
normalDrawable=resources.getDrawable(color[1]);
}
stateListDrawable.addState(pressState, pressDrawable); // 按下状态显示按下的Drawable
stateListDrawable.addState(normalState, normalDrawable);// 正常状态显示正常的Drawable
return stateListDrawable;
}
public static Drawable createColorShape(int color, int radius) {
GradientDrawable gradientDrawable = new GradientDrawable(); // 创建一个图形Drawable
gradientDrawable.setShape(GradientDrawable.RECTANGLE); // 设置图形为矩形
gradientDrawable.setCornerRadius(radius); // 设置矩形的圆角
gradientDrawable.setColor(Color.parseColor("#FFFFFF")); // 设置矩形的颜色
gradientDrawable.setStroke(1, color);
return gradientDrawable;
}
int[] pressState = {android.R.attr.state_checked};
int[] color = {Color.parseColor("#1C8FFF"), Color.parseColor("#999999")};
btn1.setBackground(Utils.createColorShapeSelector(pressState, color, 0,null));
2.设置文字颜色
public static ColorStateList createColorSelector(int[][] pressState, int[] color) {
ColorStateList colorStateList = new ColorStateList(pressState, color);
return colorStateList;
}
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_checked};
states[1] = new int[]{};
btn1.setTextColor(Utils.createColorSelector(states, color));
1-6转载自简书 作者:潇湘剑雨_