自定义checkbox 复选框的样式以及控制 checkbox 复选框的大小

不想你离开。 提交于 2019-12-02 05:50:26


先定定义一个selector

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

在定义一个style

 
    <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/selector_group</item>
    </style>

引用即可

 <CheckBox
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="im chinese"
        android:id="@+id/cb_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/CustomCheckboxTheme" />


自定义样式的复选框样式基本完成,在使用时发现复选框比较大,看起来比较丑,可通过scaleX 与scaleY 来完成,如下。

 <CheckBox
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_gravity="center_horizontal"
        android:textSize="30sp"
        android:text="test"
        android:scaleX="0.8"
        android:scaleY="0.8"
        android:id="@+id/cb_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/CustomCheckboxTheme" />

效果图 如下:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!