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);
    }

效果圖

 

 

 

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