Android CheckBox自定义样式

1.CheckBox自定义样式

1.1自定义选中颜色

   android:buttonTint="@color/colorPrimary"

1.2自定义选中图片

关键属性 :android:button

布局文件

  <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/base_checkbox_icon_selector"
        android:checked="true"
        android:text="hello" />

drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/base_icon_checked" android:state_checked="true" />
    <item android:drawable="@mipmap/base_icon_unchecked" android:state_checked="false" />
</selector>

效果图

1.3修改自定义图片和字体的间距

关键属性:android:paddingLeft


    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/base_checkbox_icon_selector"
        android:paddingLeft="10dp"
        android:checked="true"
        android:text="hello" />

效果图

1.4修改自定义图片的大小

这部分需要使用Java代码动态设置

前提是将android:button属性设置为@null

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@null"
        android:checked="true"
        android:text="hello" />

java代码

    private void setCheckBox() {
        //取得设置好的drawable对象
        Drawable drawable = getDrawable(R.drawable.base_checkbox_icon_selector);
        //设置drawable对象的大小
        drawable.setBounds(0, 0, 60, 60);
        mCheckBox .setCompoundDrawablePadding(10);
        //设置CheckBox对象的位置,对应为左、上、右、下
        mCheckBox.setCompoundDrawables(drawable, null, null, null);
    }

效果图

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章