Android版本:如何更改复选框的大小?
安卓android 复选框checkbox
我想提出的CheckBox有点小/大了,我该怎么办呢?
可以通过scaleX = "" 和scaleY=""属性来设置
- <CheckBox
- android:scaleX="0.6"
- android:scaleY="0.6"
- android:id="@+id/iv_checkbox"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:button="@drawable/checkbox"
- android:onClick="onClick" />
1. 你只需要设置相关的可绘制对象,并设置它们的复选框:
- <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" />
诀窍是如何设置的可绘制对象。下面是关于这个一个很好的教程。
2. 与API级别11开始有另一种方法存在:
- <CheckBox
- ...
- android:scaleX="0.70" // 我的直接加了这两就好了
- android:scaleY="0.70"
- />
3. 更新:这只能从API的17起工程... 要添加到另一个辉煌的答案已经给出,只能使复选框,小到文字大小允许。 按我对这个问题的回答: -我们如何能减少核取方块的大小,请一个想法CheckBox
从文本以及图像源于它的高度。 在你的XML设置这些属性:
- android:text=""
- android:textSize="0sp"
当然,如果你想没有文本(工作这仅适用 没有这些改变,CheckBox
大约是我的形象幅度较大 CodeGo.net,由乔
4. 本教程介绍了如何做到这一点链接土特
5. 我找到了一个办法做到这一点,而无需创建自己的图像。换句话说,该系统的图像被缩放。我不会假装,该解决方案是完美的,如果有人知道的方式来缩短的步骤,我会很高兴地找出如何。 首先,我把下列项目(WonActivity)的主要活动类。这是直接从堆栈溢出-谢谢你们!
- /** 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);
- }
第二,我创建了一个类“缩放绘制”。请注意,它从标准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);
- }
- }