Android学习之复选框checkbox自定义样式以及调整图片大小

1.自定义样式:

下载复选框样式图:https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2

图片重命名为英文(否则找不到)并复制到mipmap文件夹下,(切换至Project目录模式)选择xxxhdpi子文件夹。

@drawable/bg_checkbox"为drawable文件夹下新建的文件,new一个Drawable Resource File

bg_checkbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@mipmap/no"></item>
    <item android:state_checked="true" android:drawable="@mipmap/yes"></item>
</selector>

1.1:方法1:checkbox添加一行:android:button="@drawable/bg_checkbox"

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/bg_checkbox"
    android:text="吃饭"
    android:textSize="24sp"
    />

1.2:方法2:checkbox添加一行:style="@style/MyCheckBox"

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    style="@style/MyCheckBox"
    android:text="睡觉"/>

res/style.xml:

<style name="MyCheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:button">@drawable/bg_checkbox</item>
</style>

2.调整图片大小方法:缩放整体后调整位置

2.1:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/bg_checkbox"
    android:text="吃饭"
    android:textSize="30sp"
    android:scaleX="0.8"//横向缩小为原来的0.8倍
    android:scaleY="0.8"//纵向缩小为原来的0.8倍
    android:layout_marginLeft="-10dp"//左移10dp
    />

2.2:

<style name="MyCheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:button">@drawable/bg_checkbox</item>
    <item name="android:scaleX">0.8</item>
    <item name="android:scaleY">0.8</item>
</style>

 

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-2dp">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/bg_checkbox"
            android:scaleX="0.8"
            android:scaleY="0.8"
            />
        <TextView
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃饭"
            android:textSize="24sp"
            android:textColor="#000"
            android:textAlignment="center"
            android:layout_marginLeft="100dp"
            />
    </LinearLayout>

猜你喜欢

转载自blog.csdn.net/lzp1467188465/article/details/108437651