Android:如何更改CheckBox大小?

时间:2010-01-27 23:45:15

标签: android checkbox

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

13 个答案:

答案 0 :(得分:130)

从API Level 11开始,还有另一种方法:

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

答案 1 :(得分:72)

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

>>> df.columns = df.columns.astype(int)
>>> df.ix[:, 2014]
Region 10    10.8
Georgia       7.1
Name: 2014, dtype: float64

结果:

  

<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并在父布局上添加点击侦听器,然后以编程方式触发复选框来设置文本复选框。

答案 2 :(得分:55)

您只需设置相关的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" />

诀窍在于如何设置drawables。 Here's a good tutorial about this

答案 3 :(得分:11)

我已经找到了很多答案,但是当我们需要带有我的用户界面enter image description here

中的复选框的文字时,它们可以正常工作而没有文字

根据我的UI要求,我不能不增加TextSize,所以我尝试的其他选项是scaleX和scaleY(Strach复选框)和自定义xml选择器与.png图像(它也会产生不同屏幕尺寸的问题)< / p>

  

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

分3步完成。

第1步:将Vector 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>

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>

注意如果您正在使用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>

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

现在就像:

enter image description here

  

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

希望它会有所帮助!

答案 4 :(得分:8)

我用

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

调整复选框的大小

然后我设置这样的边距

android:layout_marginLeft="-10dp"

调整复选框的位置。

答案 5 :(得分: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);

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

答案 6 :(得分:4)

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


要添加已经给出的其他精彩答案,您只能将复选框缩小到文字大小允许的范围。

根据我对这个问题的回答:   - how can we reduce the size of checkbox please give me an idea


CheckBox从TEXT和图像中获得高度。

在XML中设置这些属性:

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

当然,只有在你不需要文字(为我工作)时才有效。

如果没有这些变化,CheckBox在我的形象周围给了我很大的空间,正如Joe所提到的那样

答案 7 :(得分:0)

我找到了一种方法,无需创建自己的图像。换句话说,系统图像正在缩放。我不假装解决方案是完美的;如果有人知道缩短某些步骤的方法,我会很高兴知道如何。

首先,我将以下内容放在项目的主要活动类(WonActivity)中。这是直接从Stack Overflow中获取的 - 谢谢你们

/** get the default drawable for the check box */
Drawable getDefaultCheckBoxDrawable()
{
  int resID = 0;

  if (Build.VERSION.SDK_INT <= 10)
  {
    // pre-Honeycomb has a different way of setting the CheckBox button drawable
    resID = Resources.getSystem().getIdentifier("btn_check", "drawable", "android");
  }
  else
  {
    // starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable
    TypedValue value = new TypedValue();
    getApplicationContext().getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);
    resID = value.resourceId;
  }

  return getResources().getDrawable(resID);
}

其次,我创建了一个“缩放drawable”的类。请注意,它与标准ScaleDrawable完全不同。

import android.graphics.drawable.*;

/** The drawable that scales the contained drawable */

public class ScalingDrawable extends LayerDrawable
{
  /** X scale */
  float scaleX;

  /** Y scale */
  float scaleY;

  ScalingDrawable(Drawable d, float scaleX, float scaleY)
  {
    super(new Drawable[] { d });
    setScale(scaleX, scaleY);
  }

  ScalingDrawable(Drawable d, float scale)
  {
    this(d, scale, scale);
  }

  /** set the scales */
  void setScale(float scaleX, float scaleY)
  {
    this.scaleX = scaleX;
    this.scaleY = scaleY;
  }

  /** set the scale -- proportional scaling */
  void setScale(float scale)
  {
    setScale(scale, scale);
  }

  // The following is what I wrote this for!

  @Override
  public int getIntrinsicWidth()
  {
    return (int)(super.getIntrinsicWidth() * scaleX);
  }

  @Override
  public int getIntrinsicHeight()
  {
    return (int)(super.getIntrinsicHeight() * scaleY);
  }
}

最后,我定义了一个复选框类。

import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.widget.*;

/** A check box that resizes itself */

public class WonCheckBox extends CheckBox
{
  /** the check image */
  private ScalingDrawable checkImg;

  /** original height of the check-box image */
  private int origHeight;

  /** original padding-left */
  private int origPadLeft;

  /** height set by the user directly */
  private float height;

  WonCheckBox()
  {
    super(WonActivity.W.getApplicationContext());
    setBackgroundColor(Color.TRANSPARENT);

    // get the original drawable and get its height
    Drawable origImg = WonActivity.W.getDefaultCheckBoxDrawable();
    origHeight = height = origImg.getIntrinsicHeight();
    origPadLeft = getPaddingLeft();

    // I tried origImg.mutate(), but that fails on Android 2.1 (NullPointerException)
    checkImg = new ScalingDrawable(origImg, 1);
    setButtonDrawable(checkImg);
  }

  /** set checkbox height in pixels directly */
  public void setHeight(int height)
  {
    this.height = height;
    float scale = (float)height / origHeight;
    checkImg.setScale(scale);

    // Make sure the text is not overlapping with the image.
    // This is unnecessary on Android 4.2.2, but very important on previous versions.
    setPadding((int)(scale * origPadLeft), 0, 0, 0);

    // call the checkbox's internal setHeight()
    //   (may be unnecessary in your case)
    super.setHeight(height);
  }
}

就是这样。如果在视图中放置WonCheckBox并应用setHeight(),则复选框图像的大小将合适。

答案 8 :(得分:0)

使用此代码。

布局:

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/my_checkbox"  *** here
        android:checked="true"/>

添加一个新的drawable: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 drawable:

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

答案 9 :(得分:0)

您可以尝试以下解决方案,通过在布局文件中将以下属性设置为Checkbox来更改自定义复选框的大小。为我工作

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>

答案 10 :(得分:0)

假设您的原始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>

然后只需在您的复选框xml中删除android:button="@drawable/xml_above",并在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);
}

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

答案 11 :(得分:0)

我找不到我想要的相关答案。因此,此答案适用于带有以下文本的复选框,您要分别调整复选框可绘制对象和文本的大小。

enter image description here

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

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

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

答案 12 :(得分:0)

如果要将自定义图像添加到复选框,则 将按钮设置为null并仅将背景添加到复选框 解决了

 <CheckBox
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_marginLeft="-10dp"
    android:button="@null"
    android:background="@drawable/memory_selector"/>