首先:大小由Java代码来设置;颜色由图片来决定;
注意:如果有人的checkbox太大或太小,应该是选择的drawable文件夹不对,我这个是大尺寸的,放在了drawable-xxhdpi下。如果你用自己的图片,请根据美工做图的大小来放到相应的drawable下。
准备:两张用作CheckBox选中和没有选中状态的图片到res的drawable-xxhdpi中,名称可以设置如下:
checkbox_checked.png
checkbox_normal.png
【第一种简单的方法:】
1、在res/drawable-xxhdpi中添加checkbox_style.xml,用于定义checkbox的drawable图片。
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<item
android:drawable=
"@drawable/checkbox_checked"
android:state_checked=
"true"
/>
<item
android:drawable=
"@drawable/checkbox_normal"
android:state_checked=
"false"
/>
<item
android:drawable=
"@drawable/checkbox_normal"
/>
</selector>
|
来自CODE的代码片
checkbox_style.xml
2、在values文件夹下的styles.xml文件中添加MyCheckBox样式
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<style
name=
"MyCheckBox"
parent=
"@android:style/Widget.CompoundButton.CheckBox"
>
<item
name=
"android:button"
>@drawable/checkbox_style
</item>
</style>
|
来自CODE的代码片
styles.xml
3、在layout布局文件中使用MyCheckBox样式
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<CheckBox
android:id=
"@+id/select_all"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
style=
"@style/MyCheckBox"
/>
|
来自CODE的代码片
activity_main.xml
【第二种稍微复杂的方法:】
注:关键点在于设置
复制代码1
android:button="@null"
以及
复制代码1
drawable.setBounds(
0,0,40,40)
;复制代码checkBox.setCompoundDrawables(drawable,null,null,null);

二、在res/drawable-xxhdpi中添加checkbox_style.xml,用于定义checkbox的drawable图片。
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<item
android:drawable=
"@drawable/checkbox_checked"
android:state_checked=
"true"
/>
<item
android:drawable=
"@drawable/checkbox_normal"
android:state_checked=
"false"
/>
<item
android:drawable=
"@drawable/checkbox_normal"
/>
</selector>
|
来自CODE的代码片
checkbox_style.xml
三、在Layout中修改checkbox的属性,注意android:button="@null"是自定义的关键。
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<CheckBox
android:id=
"@+id/checkBox"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:button=
"@null"
android:text=
"选择框文字"
android:textSize=
"15sp"
/>
|
来自CODE的代码片
activity_main.xml
四、在Activity中的Java代码中设置大小和位置
复制代码1 2 3 4 5 6 7 8 9 10 11
1
2
3
4
5
6
7
8
9
10
11
|
复制代码1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//取得CheckBox对象
CheckBox
checkBox
=
(
CheckBox
)
findViewById
(
R
.
id
.
checkBox
);
//取得设置好的drawable对象
Drawable
drawable
=
this
.
getResources
().
getDrawable
(
R
.
drawable
.
checkbox_style
);
//设置drawable对象的大小
drawable
.
setBounds
(
0
,
0
,
40
,
40
);
//设置CheckBox对象的位置,对应为左、上、右、下
checkBox
.
setCompoundDrawables
(
drawable
,
null
,
null
,
null
);
|
来自CODE的代码片
MainActivity.java
需要调整颜色时直接替换图片即可,需要调整大小和位置时直接在代码里改动就可以了。
最后
以上就是贤惠花生最近收集整理的关于android设置修改CheckBox框大小、颜色的两种方法的全部内容,更多相关android设置修改CheckBox框大小、颜色内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复