通常selector 都是在drawable/color文件夹中定义好,但有时候一些特殊需求需要我们动态通过代码去更改,这个时候就要用到StateListDrawable 和 ColorStateList 了,这两个分别是设置图片和颜色的类
1、对不同状态的控件设置不同的图片(StateListDrawable)
通常我们在设置不同状态下图片不同时是这么写的:比如checkBox
a、定义一个selector文件
<?xml version= "1.0" encoding="utf-8">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_toolbar_huabuwan_selected" android:state_checked="true"/>
<item android:drawable="@drawable/ic_toolbar_huabuwan_normal" android:state_checked="false"/>
</selector>
b、然后设置给checkBox
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/selector_check"/>
这是在图标没有定死的情况下,如果图标是要动态替换则需要通过下面这种方式了
2、通过代码动态设置图标
/**
* 设置底部tab图标
* @paramradioButton控件
* @paramdrawableNormal常态时的图片
* @paramdrawableSelect选中时的图片
*/
public void setSelectorDrawable(CheckBox cbButton,Drawable drawableNormal,Drawable drawableSelect){
StateListDrawable drawable =newStateListDrawable();
//选中
drawable.addState(new int[]{android.R.attr.state_checked},drawableSelect);
//未选中
drawable.addState(new int[]{-android.R.attr.state_checked},drawableNormal);
cbButton.setBackgroundDrawable(drawable);
}
同理selector的颜色也是如此设置,设置颜色的类是ColorStateList
/**
* 设置底部tab文字颜色
* @paramradioButton控件
* @paramnormal正常时的颜色值
* @paramchecked选中时的颜色值
*/
public void setSelectorColor(RadioButton radioButton,intnormal,intchecked){
int[] colors =new int[] { normal, checked,normal};
int[][] states =new int[3][];
states[0] =new int[] { -android.R.attr.state_checked};
states[1] =new int[] { android.R.attr.state_checked};
states[2] =new int[] {};
ColorStateList colorStateList =newColorStateList(states,colors);
radioButton.setTextColor(colorStateList);
}
注意:-android.R.attr.state_checked 和 android.R.attr.state_checked 的区别在于 “-” 号代表值里的true 和 false ,有“-”为false 没有则为true
网友评论
int height_item = 100;
int out_radius = 8;
int strokeWidth = 3;
int out_radius_2 = 5;
GradientDrawable bgGradientDrawable = new GradientDrawable();
bgGradientDrawable.setSize(width_item, height_item);
bgGradientDrawable.setStroke(strokeWidth, Color.parseColor("#ff0000"));
float[] outerR = new float[] { out_radius, out_radius, out_radius, out_radius, out_radius, out_radius,
out_radius, out_radius };
bgGradientDrawable.setCornerRadii(outerR);
bgGradientDrawable.setShape(GradientDrawable.RECTANGLE);
GradientDrawable shapeDrawable = new GradientDrawable();
shapeDrawable.setSize(width_item-16, height_item-16);
shapeDrawable.setStroke(0, 0);
float[] outerR_2 = new float[] { out_radius_2, out_radius_2, out_radius_2, out_radius_2, out_radius_2,
out_radius_2, out_radius_2, out_radius_2 };
shapeDrawable.setCornerRadii(outerR_2);
shapeDrawable.setShape(GradientDrawable.RECTANGLE);
shapeDrawable.setColor(Color.parseColor("#fff000"));
LayerDrawable layerDrawableSel = new LayerDrawable(new Drawable[] { bgGradientDrawable, shapeDrawable });
layerDrawableSel.setLayerInset(1, 8, 8, 8, 8);
LayerDrawable layerDrawableUnsel = new LayerDrawable(new Drawable[] { shapeDrawable });
layerDrawableUnsel.setLayerInset(0, 8, 8, 8, 8);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, layerDrawableSel);
stateListDrawable.addState(new int[] { -android.R.attr.state_pressed }, layerDrawableUnsel);
stateListDrawable.addState(new int[] { android.R.attr.state_selected }, layerDrawableSel);
stateListDrawable.addState(new int[] { -android.R.attr.state_selected }, layerDrawableUnsel);
imageView.setBackground(stateListDrawable);
imageView.setSelected(true);