android 动态设置textview selector,android.R.attr.state_selected textview无效
TextView tv = (TextView) View.inflate(parent.getContext(), itemResId, null);tv.setPadding(11 * oneDp, 7 * oneDp, 11 * oneDp, 7 * oneDp);{GradientDrawable drawableDef
·
TextView tv = (TextView) View.inflate(parent.getContext(), itemResId, null);
tv.setPadding(11 * oneDp, 7 * oneDp, 11 * oneDp, 7 * oneDp);
{
GradientDrawable drawableDefault = new GradientDrawable();
drawableDefault.setShape(GradientDrawable.RECTANGLE);
drawableDefault.setCornerRadius(16 * oneDp);
drawableDefault.setStroke(oneDp / 2, ContextCompat.getColor(parent.getContext(), R.color.user_tag_gray_line_color));
drawableDefault.setColor(ContextCompat.getColor(parent.getContext(), android.R.color.transparent));
GradientDrawable drawableChecked = new GradientDrawable();
drawableChecked.setShape(GradientDrawable.RECTANGLE);
drawableChecked.setCornerRadius(16 * oneDp);
drawableChecked.setColor(colorSelectedBg);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_checked}, drawableChecked);
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawableChecked);
stateListDrawable.addState(new int[]{}, drawableDefault);
tv.setBackground(stateListDrawable);
}
tv.setText(tag);
注意点
- stateListDrawable.addState(new int[]{}, drawableDefault); 放到addState的最后;
- TextView android.R.attr.state_selected 是无效的,使用checked
更多推荐
已为社区贡献4条内容
所有评论(0)