RadioGroup和CheckBox设置背景以及4.4系统上隐藏默认样式的坑

原创文章,如有转载,请注明出处:http://blog.csdn.net/myth13141314/article/details/72926013


RadioGroup

  • 设置动态变化的背景
radioButton.setBackground(getResources().getDrawable(R.drawable.selector_rbtn_right_red_gray));
  • 设置动态变化的字体颜色
radioButton.setTextColor(getResources().getColorStateList(R.color.rb_text_color));
  • 编写color文件,color文件不同于drawable文件,需要单独在values文件夹下面新建color文件夹
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/std_c10"/>
    <!-- not selected -->
    <item android:state_checked="false" android:color="@color/std_c7"/>
</selector>
隐藏RadioGroup的默认样式
  • 隐藏默认样式的方式:需要设置setButtonDrawable为透明色
radioButton.setButtonDrawable(android.R.color.transparent);
  • 或者直接设置布局里的属性
android:button="@null"(无论4.4还是高版本都有效)
  • 4.4系统上隐藏默认样式的坑:setButtonDrawable设置为null,在4.4及以下的系统上无效
radioButton.setButtonDrawable(null);

CheckBox

  • 设置背景
<CheckBox
    android:id="@+id/cbx_select_all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@null"
    android:layout_marginRight="5dp"
    android:background="@drawable/selector_checkbox_bg"/>
  • 选中和未选中的背景
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@drawable/checkbox_select_icon"/>
    <item android:state_checked="true" android:drawable="@drawable/checkbox_select_icon"/>
    <item android:state_checked="false" android:drawable="@drawable/checkbox_unselect_icon"/>
    <item android:state_selected="false" android:drawable="@drawable/checkbox_unselect_icon"/>
    <item android:drawable="@drawable/checkbox_unselect_icon"/>
</selector>


欢迎关注我的公众号,和我一起每天进步一点点!
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值