一手遮天 Android - view(选择类): CheckBox 样式

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(选择类): CheckBox 样式

示例如下:

/view/selection/CheckBoxDemo2.java

/**
 * CheckBox - 复选框控件
 *     setButtonDrawable() - 设置复选框图片在各种状态下的样式
 *
 *
 * 本例主要介绍 CheckBox 的样式
 */

package com.webabcd.androiddemo.view.selection;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;

import com.webabcd.androiddemo.R;

public class CheckBoxDemo2 extends AppCompatActivity {

    private CheckBox _checkBox3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_selection_checkboxdemo2);

        _checkBox3 = (CheckBox)findViewById(R.id.checkBox3);
        _checkBox3.setButtonDrawable(R.drawable.selector_checkbox_button);
    }
}

/layout/activity_view_selection_checkboxdemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        CheckBox - 复选框控件
            button - 设置复选框图片在各种状态下的样式(参见 drawable/selector_checkbox_button.xml)
    -->

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:button="@drawable/selector_checkbox_button"
        android:text="checkBox1" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_checkbox_button"
        android:text="checkBox2" />

    <!--
        在 java 中设置 CheckBox 的 button
        调整复选框的图标与文字的间距可以通过 padding 来实现
    -->
    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="checkBox2" />

</LinearLayout>

/drawable/selector_checkbox_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        state_enabled="true" - 可用
        state_enabled="false" - 不可用
        state_checked="true" - 选中
        state_checked="false" - 未选中


        注意:可以直接在 item 的 drawable 指定图片,但是这样只能按照原图的大小显示,如果需要指定图片的显示大小的话,则需要像本例这样使用 layer-list
    -->

    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:drawable="@drawable/layerlist_checkbox_button_checked"/>
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:drawable="@drawable/layerlist_checkbox_button_unchecked" />
</selector>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 13:02  webabcd  阅读(84)  评论(0编辑  收藏  举报