动态绘制CheckedTextView
16lz
2021-01-23
这几天需要用到一个动态的多选列表框,结果从资源文件中设置是没有问题的。
例1:
<CheckedTextView android:id="@+id/checkedTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:text="@string/loansmanager" />
于是我为了动态生成,在java代码中这样设置
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);
更多相关文章
- android 常用代码
- 常用的android权限配置和常用工具代码
- Android终于公布源代码
- Android常用代码
- android 蓝牙打印程序源代码
- [Android]代码实现StateListDrawable
- Android仿计算器布局代码
- linux和windows平台下下载android sdk的源代码【Z】
- Android修改源代码控制不锁屏