Android:如何更改CheckBox大小?

ale*_*2k8 86 checkbox android

我想让CheckBox更小/更大,我该怎么做?

ale*_*2k8 133

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

<CheckBox
    ...
    android:scaleX="0.70"
    android:scaleY="0.70"
/>
Run Code Online (Sandbox Code Playgroud)

  • 此技术还将缩放文本标签.为了弥补这一点,我调整了文本大小如下checkBox.TextSize =(int)(TEXT_SIZE/SCALE_FACTOR); (5认同)
  • 如果我这样做增加,例如将其缩放到"2.0"然后它会变大,但图像被剪裁,我看到复选框的右半部分和文本的左半部分.有什么方法吗? (5认同)
  • 不错,它仍然需要相同的空间.要解决我使用-ve margin,如:android:layout_marginLeft =" - 10dp" (4认同)

Ant*_*lvy 82

这是一个更好的解决方案,不会剪切和/或模糊drawable,但只有当复选框本身没有文本时才有效(但你仍然可以有文本,它只是更复杂,最后看到).

<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"/>
Run Code Online (Sandbox Code Playgroud)

结果:

什么以前的解决方案与scaleXscaleY看起来像:

您可以通过TextView在其旁边添加一个文本复选框并在父布局上添加单击侦听器,然后以编程方式触发复选框.

  • 这是一个比接受的答案要好得多的答案. (3认同)
  • 如果您的复选框带有文本,则无法使用背景。 (2认同)

mor*_*aes 56

您只需设置相关的drawable并在复选框中设置它们:

<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" />
Run Code Online (Sandbox Code Playgroud)

诀窍是如何设置drawables.这是一个很好的教程.

  • 请记住,不同的设备可能已安装自定义主题.如果您通过放置自己的自定义图片来调整大小,请确保替换应用中的每个复选框(即使没有更改大小),否则您可能会在某些设备上混合使用各种样式. (11认同)
  • 链接已损坏 (3认同)

Aje*_*ary 11

好吧,我已经找到了很多答案,但是当我们需要文本以及我的UI中的复选框时,它们在没有文本的情况下工作正常在此输入图像描述

这里根据我的UI要求,我不能不增加TextSize所以我尝试的其他选项是scaleX和scaleY(Strach复选框)和自定义xml选择器与.png图像(它也创建不同屏幕大小的问题)

但我们有另一种解决方案,即Vector Drawable

分3步完成.

第1步:将Vector Drawable复制到您的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>
Run Code Online (Sandbox Code Playgroud)

un_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,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>
Run Code Online (Sandbox Code Playgroud)

(注意如果您正在使用Android Studio,您也可以在那里添加这些Vector Drawable表单,右键单击您的drawable文件夹,然后单击New/Vector Asset,从那里选择这些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>
Run Code Online (Sandbox Code Playgroud)

第3步:将drawable设置为复选框

<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" />
Run Code Online (Sandbox Code Playgroud)

现在它像:

在此输入图像描述


您也可以更改它的宽度和高度或viewportHeight和viewportWidth以及fillColor


希望它会有所帮助!

  • 就我而言,我必须设置 `android:button="@null"` 和 `android:background="@drawable/check_box_selector"` 以调整复选框大小。 (2认同)

小智 9

我用

android:scaleX="0.70" android:scaleY="0.70"

调整复选框的大小

然后我设置这样的边距

android:layout_marginLeft="-10dp"
Run Code Online (Sandbox Code Playgroud)

调整复选框的位置.


Sag*_*gar 7

您可以尝试以下解决方案,通过在布局文件中custom checkbox设置以下属性来更改大小。Checkbox为我工作

机器人:scaleX =“0.8”机器人:scaleY =“0.8”

android:button="@null"
android:scaleX="0.8"
android:scaleY="0.8"
android:background="@drawable/custom_checkbox"
Run Code Online (Sandbox Code Playgroud)

将以下行添加到可绘制文件中

<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>
Run Code Online (Sandbox Code Playgroud)


Chr*_*Zou 6

这是我做的,第一集:

android:button="@null"

并设定

android:drawableLeft="@drawable/selector_you_defined_for_your_checkbox"
Run Code Online (Sandbox Code Playgroud)

然后在你的Java代码中:

Drawable d = mCheckBox.getCompoundDrawables()[0];
d.setBounds(0, 0, width_you_prefer, height_you_prefer);
mCheckBox.setCompoundDrawables(d, null, null, null);
Run Code Online (Sandbox Code Playgroud)

它对我有用,希望它对你有用!