1.使用StateListDrawable替换selector
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; }
默认不好使需要在使用的对象设置为可点击的;有两种方法:
方法1
tv.setClickable(true);
方法2:
tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { UIUtils.toast(tv.getText().toString(),false); } });
2.使用GradientDrawable替换shape
public static Drawable getDrawable(int rgb,float radius){ GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(rgb); //填充颜色 gradientDrawable.setGradientType(GradientDrawable.RECTANGLE); //shape矩形 gradientDrawable.setCornerRadius(radius); //四周圆角半径 gradientDrawable.setStroke(UIUtils.dp2px(1), rgb); //边框厚度与颜色 return gradientDrawable; }