安卓:如何改变复选框的大小?

117

我想让复选框变得更小/更大,应该如何实现?

13个回答

167

从API Level 11开始,存在另一种方法:

<CheckBox
    ...
    android:scaleX="0.70"
    android:scaleY="0.70"
/>

6
这种技术也会缩放文本标签。为了弥补这一点,我按以下方式调整了文本大小:checkBox.TextSize = (int)(TEXT_SIZE/SCALE_FACTOR); - samus
7
如果我把它增加,例如缩放到“2.0”,那么它确实会变大,但图像会被裁剪,我只能看到复选框的右半部分和文本的左半部分。有什么解决方法吗? - Al Lelopath
4
不错,但它仍然占用相同的空间。为了解决这个问题,我使用了负边距,例如:android:layout_marginLeft="-10dp"。 - M. Usman Khan
我可能不会在IDE的预览中显示,但CheckBox将在运行时缩放。 - Leo DroidCoder
在AS设计视图中显示。这是一个本不需要修复的简单解决方案。我认为这是API或设备的影响。使用Gennymotion设备在API 22上,看起来0.8是水平LinearLayout的最大Y值,而廉价的Onix平板电脑则不会在1处裁剪。 - MikeT
这对我调整复选框大小很有帮助...非常感谢,alex2k8。 - amyShamna

112

这里有一个更好的解决方案,它不会剪裁和/或模糊可绘制对象,但仅适用于复选框本身没有文本的情况(但您仍然可以有文本,只是更加复杂,请参见结尾部分)。

<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"/>

结果:

之前使用scaleXscaleY的解决方案如下所示:

您可以通过在旁边添加一个TextView并在父布局上添加点击监听器,然后以编程方式触发复选框来拥有一个文本复选框。


2
如果你的复选框有文本,使用背景是行不通的。 - Zach Green
@ZachGreen 是的,确实如此。我没有考虑到复选框上有文本的情况,我的没有。我会更新答案。 - Antoine
5
这个回答比被采纳的那个更好。 - vtlinh
在160dp的情况下看起来很模糊。似乎分辨率太低了。 - Gianluca Demarinis
1
如何为此复选框设置色调? - YTerle
显示剩余3条评论

57

你只需要设置相关的可绘制内容并将它们设置在复选框中:

<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" />

关键在于如何设置可绘制对象。这是一篇很好的教程,可以了解更多相关内容。


14
请注意,不同的设备可能安装了自定义主题。如果您通过放置自己的自定义图像来调整大小,请确保替换应用程序中的每个复选框(即使没有尺寸更改),否则在某些设备上可能会导致混合风格。 - DougW
9
链接已损坏。 - Marvin

26

我找到了很多答案,但当我们需要复选框和文本一起工作时,它们在没有文本的情况下可以很好地工作,就像在我的UI中:enter image description here

根据我的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" />

现在是这样的:

enter image description here


您可以更改其宽度和高度或viewportHeight和viewportWidth以及fillColor


希望这可以帮到您!


2
在我的情况下,我不得不设置 android:button="@null"android:background="@drawable/check_box_selector" 以使它调整复选框的大小。 - Artur Szymański
这种方法会破坏填充。 - Oleh Liskovych

12
我使用 android:scaleX="0.70" android:scaleY="0.70" 来调整复选框的大小,然后我像这样设置边距。
android:layout_marginLeft="-10dp"

调整复选框的位置。


兄弟,虽然它确实缩小了尺寸,但在我的情况下边距不起作用。我的复选框图像为173 x 93 - 复选框的大小仍占据该空间。Antoine Bolvy是我认为更灵活的解决方案。 - Alexey Shevelyov

7
您可以尝试以下解决方案来更改“自定义复选框”的大小,通过在布局文件中将以下属性设置为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>

6

以下是我所做的,首先设置:

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);

这对我有用,希望对你也有用!


4
更新: 仅从API 17开始才有效...

除了其他已给出的杰出答案外,您只能使复选框尽可能小,以适应文本大小。

根据我在这个问题上的回答: - 如何减小复选框的大小,请给我一个想法


CheckBox从文本和图像派生其高度。

在您的XML中设置这些属性:

android:text=""
android:textSize="0sp"

当然,这只适用于不需要文本的情况(对我而言起作用)。
没有这些更改,CheckBox 会在我的图像周围产生一个大间距,如 Joe 所提到的那样。

2
请使用这段代码。
在布局中:
<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"/>


2
我找不到符合我需求的相关答案,所以我自己想出了解决方法。以下是关于同时更改复选框和文字的大小的答案。

enter image description here

你需要两个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文件checkbox.xml。
<?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>

现在您可以按照以下方式在layout.xml中定义它:
<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 -->

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接