Android04--Android之CheckBox+RadioGroup+RadioButton

1.CheckBox

RadioButton(单选按钮)、CheckBox(复选按钮)、ToggleButton(开关按钮)都继承自 android.widget.CompoundButton类,而CompoundButton又继承自Button类,Button又继承与TextView,所以他们都具有TextView的属性.
首先我们学习CheckBox,他不依赖于其他任何的控件.

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        // 把复选款放置上面,并未它设置背景选择器
        android:drawableTop="@drawable/sel_cb"
        // 原生的复选款,默认在左侧,如果不想要,就下面这样处理.
        android:button="@null"
        android:text="Hello World" />

如果要改变复选框的样式,我们可以这样做

android:drawableTop="@mipmap/ic_launcher"
android:textColor="@color/cb_color_sel"
// 背景渲染
android:drawableTint="@color/cb_color_sel"
// 背景渲染的方式,但是这两个属性都是在Api23以后才能都使用.我们可以使用兼容包在java代码中去做同样的事情.
android:drawableTintMode="src_atop"
 CheckBox checkBox = (CheckBox)findViewById(R.id.checkbox);

// android:drawableTint="@color/cb_color_sel"
// android:backgroundTintMode="src_atop"解决版本不兼容的问题

/* left, top, right, and bottom查看源码可以知道getCompoundDrawables返回checkBox的四周的drawable */

Drawable[] drawables = checkBox.getCompoundDrawables();
 Drawable drawableTop = drawables[1];

// v4包中的兼容包,只要高于Android.6均可以使用.     DrawableCompat.setTintList(drawableTop,getResources().getColorStateList(R.color.cb_color_sel));
// DrawableCompat(Compat兼容的意思):这是一个静态方法,在非静态方法调用的时候,需要传入一个对象,静态方法调用的时候,不需要(因为静态方法没有this)
DrawableCompat.setTintMode(drawableTop, PorterDuff.Mode.SRC_ATOP );

2.RadioButton

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值