解决代码中写radiobutton颜色选择器无效问题
来源:互联网 发布:bilibili客户端mac 编辑:程序博客网 时间:2024/04/27 21:33
- 开发中业务需求总是千奇百怪(搞不懂产品经理脑壳里装的什么),有时候写布局的话可以直接在xml文件中完成,但是有时候业务需求,有些布局是是动态变化的,不是setvisibily就可完成的,这个时候就要考虑通过代码来添加子布局了。
常规使用的话:
直接在xml布局文件中写,button属性是为了去除系统自带的圆圈
<RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/rb1" android:text="按键1" android:textColor="@color/teach_type_text_selector" android:background="@drawable/teach_type_rb_selector" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb2" android:text="按键2" android:textColor="@color/teach_type_text_selector" android:background="@drawable/teach_type_rb_selector" android:button="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RadioGroup>
上面使用的背景和text颜色选择器的代码我就不贴了,背景的话就是在res资源下的drawable目录中创建相应的选择器;颜色就是在res资源下的建个color目录,然后创建相应的选择器,
在代码中实现的话
上面最关键的是我转换color资源时使用的是Resources下的getColorStateList()方法,这样才有效果的
- RadioButton mRb = new RadioButton(mContext);
- mRb.setTextColor(mContext.getResources().getColorStateList(R.color.teach_type_text_selector));
- mRb.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
- mRb.setBackground(ContextCompat.getDrawable(mContext, R.drawable.teach_type_rb_selector));
- rg.addView(mRb, mp);
0 0
- 解决代码中写radiobutton颜色选择器无效问题
- RadioButton字体颜色选择器selector
- 对RadioGroup中的RadioButton设置颜色选择器
- android中RadioButton的选择器
- 代码生成RadioButton问题
- 解决datalist中radiobutton单选的问题
- 解决css中z-index无效问题
- 解决link_to 中delete无效的问题
- 代码写时间选择器
- RadioButton文字前面那个圆圈也可以使用颜色选择器
- Android中给TextView设置颜色无效的问题
- 关于Radiobutton 改变字体颜色问题
- 在代码中修改textview的字体颜色(解决只能修改一次的问题)
- 解决eclipse Alt+/ 代码补全无效问题
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- 解决android代码中重复写findViewByid()
- 通过代码写状态选择器
- AndroidStudio中由于代码问题造成的Selector无效问题
- 部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi
- HUI-Miner:高效的高效用项集(high-utility itemset)挖掘算法
- Java编写满天星+月亮
- js数字滚动展示效果,人数统计滚动效果
- Openlayers中Google地图的加载
- 解决代码中写radiobutton颜色选择器无效问题
- 房地产投资继续乐观
- Launcher的启动过程
- Python3.6+selenium3.3+Firefox52
- 数据结构与算法-排序(三)
- Shell脚本main函数
- 如何压缩ndk的so库
- JAVA多线程之——线程中断
- 1的个数