首页 > 代码库 > 代码设置Shape和Selector

代码设置Shape和Selector

开发中经常需要使用Shape和Selector,如果每个都用xml设置的话,会占用apk大小,同时命名多了也会混乱,使用代码来设置会方便很多。

需要用到2个类:GradientDrawable和StateListDrawable

相关API:

setColor(rgb); //填充颜色
setGradientType(GradientDrawable.RECTANGLE); //shape矩形
setCornerRadius(radius); //四周圆角半径
setStroke(1,strokenColor); //边框厚度与颜色

下面提供一个工具类以及使用代码

【工具类DrawableUtils】:

public class DrawableUtils {
public static GradientDrawable getDrawable(int rgb,float radius){
GradientDrawable gradientDrawable = new GradientDrawable();

gradientDrawable.setColor(rgb);//设置颜色
gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);//设置显示的样式
gradientDrawable.setCornerRadius(radius);//设置圆角的半径
gradientDrawable.setStroke(DensityUtil.dip2px(1),rgb);//描边

return gradientDrawable;
}

public static StateListDrawable getSelector(Drawable normalDrawable,Drawable pressDrawable) {
StateListDrawable stateListDrawable = new StateListDrawable();
//给当前的颜色选择器添加选中图片指向状态,未选中图片指向状态
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, pressDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normalDrawable);
//设置默认状态
stateListDrawable.addState(new int[]{}, normalDrawable);
return stateListDrawable;
}
}

【使用】:

public void initData(String content) {
//随机打乱datas顺序
Collections.shuffle(Arrays.asList(datas));
random=new Random();
//动态创建TextView
for(int i = 0; i < datas.length; i++) {
TextView textView=new TextView(getActivity());
textView.setText(datas[i]);
//【设置textView的属性】=============================================
//字体大小
textView.setTextSize(DensityUtil.dip2px(10));
//padding
int padding = DensityUtil.dip2px(3);
textView.setPadding(padding,padding,padding,padding);
//margin
ViewGroup.MarginLayoutParams params=new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin=DensityUtil.dip2px(8);
params.topMargin=DensityUtil.dip2px(8);
params.rightMargin=DensityUtil.dip2px(8);
params.bottomMargin=DensityUtil.dip2px(8);
textView.setLayoutParams(params);

//背景颜色
int red=random.nextInt(211);
int green=random.nextInt(211);
int blue=random.nextInt(211);
int rgb= Color.rgb(red, green, blue);
//设置背景方式1:
//textView.setBackgroundColor(rgb);
//设置背景方式2:
float radius=DensityUtil.dip2px(5);
//textView.setBackground(DrawableUtils.getDrawable(rgb,radius));

//【设置textView的点击事件或者说是selector】===========================
//设置Selector
GradientDrawable normalDrawable = DrawableUtils.getDrawable(rgb, radius);
GradientDrawable pressDrawable = DrawableUtils.getDrawable(Color.WHITE, radius);
StateListDrawable selector = DrawableUtils.getSelector(normalDrawable, pressDrawable);
textView.setBackground(selector);

//设置点击响应
// textView.setClickable(true);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
}
});

//【将textView添加到FramLayout中】=====================================
flowLayout.addView(textView);
}
}

代码设置Shape和Selector