动态绘制CheckedTextView
这几天需要用到一个动态的多选列表框,结果从资源文件中设置是没有问题的。
例1:
CheckedTextView2=new CheckedTextView(this);CheckedTextView2.setText("test2");CheckedTextView2.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple);myLayout.addView(CheckedTextView2);?结果报了异常android.content.res.Resources$NotFoundException when programmatically setting android.R.attr.listChoiceIndicatorMultiple
理论上这样是没有问题的,可能在绘制时需要先取到id,在绘制吧。google了一下,解决了,关键代码如下
//根据数组id得到数组类型TypedArray ta = getBaseContext().getTheme().obtainStyledAttributes(attrs);
//初始化绘制目标
Drawable indicator = ta.getDrawable(0);
CheckedTextView1=new CheckedTextView(this);
CheckedTextView1.setText("test1");
//得到绘制目标后放入选择框中
CheckedTextView1.setCheckMarkDrawable(indicator);
?