一,动态设置TextView的颜色
/**
* 设置TextView文字颜色
* @param textView 需要改变颜色的view
* @param normal 正常情况下,显示的颜色
* @param selected 选择时,显示的颜色
*/
public static void setSelectorColor(TextView textView,int normal,int selected){
int[] colors =new int[] { normal, selected,normal};
int[][] states =new int[3][];
states[0] =new int[] { -android.R.attr.state_selected};
states[1] =new int[] { android.R.attr.state_selected};
states[2] =new int[] {};
ColorStateList colorStateList =new ColorStateList(states,colors);
textView.setTextColor(colorStateList);
}
二,动态设置TextView的drawable
TextView mWifiReconnectImage;
Drawable drawable = null;
if (themes.equals("blueSky")){
drawable = getResources().getDrawable(R.drawable.video_reconnect_select_blue);
}else {
drawable = getResources().getDrawable(R.drawable.video_reconnect_select);
}
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
mWifiReconnectImage.setCompoundDrawables(null, drawable, null, null);
三,动态设置TextView的color(RGB)
TextView mWifiReconnectImage;
int colorInt = Color.parseColor("#FFFFFF");
int[] int2Rgb = ColorUtil.int2Rgb(colorInt);
mWifiReconnectImage.setTextColor(Color.rgb(int2Rgb[0], int2Rgb[1], int2Rgb[2]));
四,动态设置SeekBar的ProgressDrawable
/**
* 动态改变seekbar的ProgressDrawable
* @param seekBar
* @param color
*/
public static void changeProgressDrawable(SeekBar seekBar, int color){
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
int layers = layerDrawable.getNumberOfLayers();
Drawable[] drawables = new Drawable[layers];
for (int i = 0; i < layers; i++) {
switch (layerDrawable.getId(i)) {
// 如果是seekbar背景
case android.R.id.background:
drawables[i] = layerDrawable.getDrawable(0);
break;
// 如果是拖动条第一进度
case android.R.id.progress:
//这里为动态的颜色值
drawables[i] = layerDrawable.getDrawable(1);
drawables[i].setColorFilter(color,SRC_ATOP);
drawables[i].setBounds(layerDrawable.getDrawable(0).getBounds());
break;
}
}
seekBar.setProgressDrawable(new LayerDrawable(drawables));
seekBar.invalidate();
}
五,动态设置shape的颜色
/**
* 动态改变shape的颜色
* @param imageView
* @param color
*/
public static void changeShapeColor(ImageView imageView, int color){
GradientDrawable imageViewGrad = (GradientDrawable)imageView.getBackground();
imageViewGrad.setStroke(1,color);
imageViewGrad.setColor(color);
}
六,动态设置TextView的图标
/**
* 设置TextView图标
* @param textView
* @paramd rawableNormal常态时的图片
* @paramd rawableSelect选中时的图片
*/
public static void setSelectorDrawable(TextView textView, Drawable drawableNormal, Drawable drawableSelect){
StateListDrawable drawable =new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_selected},drawableSelect);
drawable.addState(new int[]{-android.R.attr.state_selected},drawableNormal);
textView.setBackgroundDrawable(drawable);
}