我想让复选框变得更小/更大,应该如何实现?
从API Level 11开始,存在另一种方法:
<CheckBox
...
android:scaleX="0.70"
android:scaleY="0.70"
/>
这里有一个更好的解决方案,它不会剪裁和/或模糊可绘制对象,但仅适用于复选框本身没有文本的情况(但您仍然可以有文本,只是更加复杂,请参见结尾部分)。
<CheckBox
android:id="@+id/item_switch"
android:layout_width="160dp" <!-- This is the size you want -->
android:layout_height="160dp"
android:button="@null"
android:background="?android:attr/listChoiceIndicatorMultiple"/>
结果:
之前使用scaleX
和scaleY
的解决方案如下所示:
您可以通过在旁边添加一个TextView
并在父布局上添加点击监听器,然后以编程方式触发复选框来拥有一个文本复选框。
你只需要设置相关的可绘制内容并将它们设置在复选框中:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/my_checkbox_background"
android:button="@drawable/my_checkbox" />
关键在于如何设置可绘制对象。这是一篇很好的教程,可以了解更多相关内容。
我找到了很多答案,但当我们需要复选框和文本一起工作时,它们在没有文本的情况下可以很好地工作,就像在我的UI中:
根据我的UI要求,我不能增加TextSize,所以我尝试了另一个选项,即scaleX和scaleY(拉伸复选框)和带有.png图像的自定义xml选择器(它也会因不同的屏幕大小而产生问题)
但我们还有另一个解决方案,那就是矢量图形
只需3步即可完成。
Step 1: 将这三个矢量图形复制到您的drawable文件夹中
checked.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z" />
</vector>
未选中的.xml文件
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>
(请注意,如果您正在使用Android Studio,您还可以从那里添加这些矢量Drawable。右键单击drawable文件夹,然后选择新建/矢量资产,然后从那里选择这些drawable)
步骤2:为check_box创建XML选择器
check_box_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_checked="true" />
<item android:drawable="@drawable/un_checked" />
</selector>
步骤3:将该可绘制对象设置到复选框中
<CheckBox
android:id="@+id/suggectionNeverAskAgainCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:button="@drawable/check_box_selector"
android:textColor="#FF000000"
android:textSize="13dp"
android:text=" Never show this alert again" />
现在是这样的:
您可以更改其宽度和高度或viewportHeight和viewportWidth以及fillColor
希望这可以帮到您!
android:button="@null"
和 android:background="@drawable/check_box_selector"
以使它调整复选框的大小。 - Artur Szymańskiandroid:scaleX="0.70"
android:scaleY="0.70"
来调整复选框的大小,然后我像这样设置边距。android:layout_marginLeft="-10dp"
调整复选框的位置。
Checkbox
。对我有效。
android:scaleX="0.8" android:scaleY="0.8"
android:button="@null"
android:scaleX="0.8"
android:scaleY="0.8"
android:background="@drawable/custom_checkbox"
请将以下内容添加到可绘制文件中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/unchecked_img" />
<item android:state_checked="true"
android:drawable="@drawable/checked_img" />
</selector>
以下是我所做的,首先设置:
android:button="@null"
并且还要设置
android:drawableLeft="@drawable/selector_you_defined_for_your_checkbox"
然后在你的Java代码中:
Drawable d = mCheckBox.getCompoundDrawables()[0];
d.setBounds(0, 0, width_you_prefer, height_you_prefer);
mCheckBox.setCompoundDrawables(d, null, null, null);
这对我有用,希望对你也有用!
除了其他已给出的杰出答案外,您只能使复选框尽可能小,以适应文本大小。
根据我在这个问题上的回答: - 如何减小复选框的大小,请给我一个想法
CheckBox
从文本和图像派生其高度。
在您的XML中设置这些属性:
android:text=""
android:textSize="0sp"
CheckBox
会在我的图像周围产生一个大间距,如 Joe 所提到的那样。<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/my_checkbox" *** here
android:checked="true"/>
添加一个新的可绘制对象:my_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="@drawable/checkbox_off_background"/>
<item
android:state_checked="true"
android:drawable="@drawable/checkbox_on_background"/>
创建并结束2个可绘制对象:
checkbox_off_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:height="25dp" *** your size
android:width="25dp"/>
</shape>
</item>
<item android:drawable="@android:drawable/checkbox_off_background"/>
还有,checkbox_on_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:height="25dp"
android:width="25dp"/>
</shape>
</item>
<item android:drawable="@android:drawable/checkbox_on_background"/>
你需要两个PNG文件cb_checked.png和cb_unchecked.png,并将它们添加到drawable文件夹中
现在创建cb_bg_checked.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cb_checked"
android:height="22dp" <!-- This is the size of your checkbox -->
android:width="22dp" <!-- This is the size of your checkbox -->
android:right="6dp" <!-- This is the padding between cb and text -->
tools:targetApi="m"
tools:ignore="UnusedAttribute" />
</layer-list>
And, cb_bg_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item android:drawable="@drawable/cb_unchechecked"
android:height="22dp" <!-- This is the size of your checkbox -->
android:width="22dp" <!-- This is the size of your checkbox -->
android:right="6dp" <!-- This is the padding between cb and text -->
tools:targetApi="m"
tools:ignore="UnusedAttribute" />
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cb_bg_checked" android:state_checked="true"/>
<item android:drawable="@drawable/cb_bg_unchecked" android:state_checked="false"/>
</selector>
<CheckBox
android:id="@+id/checkbox_with_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@drawable/checkbox"
android:text="This is text"
android:textColor="@color/white"
android:textSize="14dp" /> <!-- Here you can resize text -->