Android:如何更改复选框大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:44:19  来源:igfitidea点击:

Android: How to change CheckBox size?

androidcheckbox

提问by alex2k8

I would like to make CheckBox a bit smaller/bigger, how can I do this?

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

采纳答案by moraes

You just need to set the related drawables and set them in the checkbox:

您只需要设置相关的可绘制对象并在复选框中设置它们:

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

The trick is on how to set the drawables. Here's a good tutorial about this.

诀窍在于如何设置可绘制对象。这是一个很好的教程

回答by alex2k8

Starting with API Level 11 there is another approach exists:

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

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

回答by Antoine Bolvy

Here is a better solution which does not clip and/or blur the drawable, but only works if the checkbox doesn't have text itself (but you can still have text, it's just more complicated, see at the end).

这是一个更好的解决方案,它不会剪辑和/或模糊可绘制对象,但仅当复选框本身没有文本时才有效(但您仍然可以有文本,只是更复杂,请参见最后)。

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

The result:

结果:

What the previous solution with scaleXand scaleYlooked like:

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

You can have a text checkbox by adding a TextViewbeside it and adding a click listener on the parent layout, then triggering the checkbox programmatically.

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

回答by Ajeet Choudhary

Well i have found many answer, But they work fine without text when we require text also with checkbox like in my UIenter image description here

好吧,我找到了很多答案,但是当我们需要文本时,它们也可以在没有文本的情况下正常工作,并且在我的 UI 中也带有复选框在此处输入图片说明

Here as per my UI requirement i can't not increase TextSize so other option i tried is scaleX and scaleY (Strach the check box) and custom xml selector with .png Images(its also creating problem with different screen size)

这里根据我的 UI 要求,我不能不增加 TextSize,所以我尝试的其他选项是 scaleX 和 scaleY(Strach the checkbox)和带有 .png 图像的自定义 xml 选择器(它也会在不同的屏幕尺寸下产生问题)

But we have another solution for that, that is Vector Drawable

但是我们有另一个解决方案,那就是Vector Drawable

Do it in 3 steps.

分3步完成。

Step 1:Copy these three Vector Drawable to your drawable folder

第 1 步:将这三个 Vector Drawable 复制到您的 drawable 文件夹中

checked.xml

已检查.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>

un_checked.xml

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>

(Note if you are workiong with Android Studio, you can also add these Vector Drawable from there, Right click on your drawable folder then New/Vector Asset, then select these drawable from there)

请注意,如果您使用 Android Studio,您还可以从那里添加这些 Vector Drawable,右键单击您的 drawable 文件夹,然后单击 New/Vector Asset,然后从那里选择这些 drawable

Step 2:Create XML selector for check_box

第 2 步:为 check_box 创建 XML 选择器

check_box_selector.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>

Step 3:Set that drawable into check box

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

Now its like:

现在它就像:

enter image description here

在此处输入图片说明



You can change its width and height or viewportHeight and viewportWidth and fillColor also

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



Hope it will help!

希望它会有所帮助!

回答by Richard

I use

我用

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

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

to ajust the size of checkbox

调整复选框的大小

then I set margins like this

然后我像这样设置边距

android:layout_marginLeft="-10dp"

to adjust ths location of the checkbox.

调整复选框的位置。

回答by Chris.Zou

Here was what I did, first set:

这是我所做的,第一组:

android:button="@null"

android:button="@null"

and also set

并设置

android:drawableLeft="@drawable/selector_you_defined_for_your_checkbox"

then in your Java code:

然后在您的 Java 代码中:

Drawable d = mCheckBox.getCompoundDrawables()[0];
d.setBounds(0, 0, width_you_prefer, height_you_prefer);
mCheckBox.setCompoundDrawables(d, null, null, null);

It works for me, and hopefully it will work for you!

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

回答by Richard Le Mesurier

UPDATE: this only works from API 17 onwards...

更新:这只适用于 API 17 以后......



To add to the other brilliant answers already given, you can only make the checkbox as small as the text size allows.

要添加到已经给出的其他精彩答案,您只能将复选框设置为文本大小允许的大小。

As per my answer on this question: - how can we reduce the size of checkbox please give me an idea

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



CheckBoxderives its height from the TEXT as well as the image.

CheckBox从文本和图像中获取其高度。

Set these properties in your XML:

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

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

Of course this only works if you want no text (worked for me).

当然,这仅在您不想要文本时才有效(对我有用)。

Without these changes, the CheckBoxwas giving me a big margin around my image, as mentioned by Joe

没有这些变化,CheckBox就像乔提到的那样,我的形象给了我很大的利润

回答by Sagar

You can try the following solution to change the size of custom checkboxby setting the following properties to Checkboxin your layout file. Worked for me

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

android:scaleX="0.8" android:scaleY="0.8"

android:scaleX="0.8" android:scaleY="0.8"

android:button="@null"
android:scaleX="0.8"
android:scaleY="0.8"
android:background="@drawable/custom_checkbox"

add following lines to drawable file

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

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

回答by Fruit

Assume your original xml is:

假设您的原始 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/tick_img" />
    <item android:state_checked="false"
        android:drawable="@drawable/untick_img" />
</selector>

then simply remove android:button="@drawable/xml_above"in your checkbox xml, and do drawable scaling programmatically in java (decrease the 150big size to your desired dp):

然后只需删除android:button="@drawable/xml_above"您的复选框xml,并在java中以编程方式进行可绘制缩放(将150大尺寸减小到您想要的dp):

CheckBox tickRememberPasswd = findViewById(R.id.remember_tick);

//custom selector size
Drawable drawableTick = ContextCompat.getDrawable(this, R.drawable.tick_img);
Drawable drawableUntick = ContextCompat.getDrawable(this, R.drawable.untick_img);
Bitmap bitmapTick = null;
if (drawableTick != null && drawableUntick != null) {
    int desiredPixels = Math.round(convertDpToPixel(150, this));

    bitmapTick = ((BitmapDrawable) drawableTick).getBitmap();
    Drawable dTick = new BitmapDrawable(getResources()
            , Bitmap.createScaledBitmap(bitmapTick, desiredPixels, desiredPixels, true));

    Bitmap bitmapUntick = ((BitmapDrawable) drawableUntick).getBitmap();
    Drawable dUntick = new BitmapDrawable(getResources()
            , Bitmap.createScaledBitmap(bitmapUntick, desiredPixels, desiredPixels, true));

    final StateListDrawable statesTick = new StateListDrawable();
    statesTick.addState(new int[] {android.R.attr.state_checked},
            dTick);
    statesTick.addState(new int[] { }, //else state_checked false
            dUntick);
    tickRememberPasswd.setButtonDrawable(statesTick);
}

the convertDpToPixelmethod:

convertDpToPixel方法:

public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

回答by Vinayak Bevinakatti

I could not find the relevant answer for my requirement which I figured it out. So, this answer is for checkbox with text like below where you want to resize the checkbox drawable and text separately.

我找不到我想出的要求的相关答案。因此,此答案适用于带有如下文本的复选框,您要在其中分别调整复选框 drawable 和文本的大小。

enter image description here

在此处输入图片说明

You need two PNGs cb_checked.png and cb_unchechecked.png add them to drawable folder

您需要两个 PNG cb_checked.png 和 cb_unchechecked.png 将它们添加到 drawable 文件夹

Now create cb_bg_checked.xml

现在创建 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

并且,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>

Then create a selector XML checkbox.xml

然后创建一个选择器 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>

Now define it your layout.xml like this

现在像这样定义你的 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 -->