Android学习之复选框checkbox自定义样式以及调整图片大小
生活随笔
收集整理的這篇文章主要介紹了
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"
<CheckBoxandroid: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"
<CheckBoxandroid: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:
<CheckBoxandroid: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>?
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="-2dp"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/bg_checkbox"android:scaleX="0.8"android:scaleY="0.8"/><TextViewandroid: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>?
總結
以上是生活随笔為你收集整理的Android学习之复选框checkbox自定义样式以及调整图片大小的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7.5 角色-侦测
- 下一篇: 2023-04-18_面试题复盘笔记(1