Android版本:如何更改复选框的大小

发布时间 2023-04-18 23:51:21作者: 十乂

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

  

安卓android 复选框checkbox
我想提出的CheckBox有点小/大了,我该怎么办呢?
可以通过scaleX = ""  和scaleY=""属性来设置  

  1. <CheckBox
  2. android:scaleX="0.6"
  3. android:scaleY="0.6"
  4. android:id="@+id/iv_checkbox"
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:button="@drawable/checkbox"
  8. android:onClick="onClick" />

1. 你只需要设置相关的可绘制对象,并设置它们的复选框:

  1. <CheckBox
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="new checkbox"
  5. android:background="@drawable/my_checkbox_background"
  6. android:button="@drawable/my_checkbox" />

诀窍是如何设置的可绘制对象。下面是关于这个一个很好的教程。 
2. 与API级别11开始有另一种方法存在:

  1. <CheckBox
  2. ...
  3. android:scaleX="0.70" // 我的直接加了这两就好了
  4. android:scaleY="0.70"
  5. />

3. 更新:这只能从API的17起工程... 要添加到另一个辉煌的答案已经给出,只能使复选框,小到文字大小允许。 按我对这个问题的回答: -我们如何能减少核取方块的大小,请一个想法CheckBox从文本以及图像源于它的高度。 在你的XML设置这些属性:

  1. android:text=""
  2. android:textSize="0sp"

当然,如果你想没有文本(工作这仅适用 没有这些改变,CheckBox大约是我的形象幅度较大 CodeGo.net,由乔 
4. 本教程介绍了如何做到这一点链接土特 
5. 我找到了一个办法做到这一点,而无需创建自己的图像。换句话说,该系统的图像被缩放。我不会假装,该解决方案是完美的,如果有人知道的方式来缩短的步骤,我会很高兴地找出如何。 首先,我把下列项目(WonActivity)的主要活动类。这是直接从堆栈溢出-谢谢你们!

  1. /** get the default drawable for the check box */
  2. Drawable getDefaultCheckBoxDrawable()
  3. {
  4. int resID = 0;
  5. if (Build.VERSION.SDK_INT <= 10)
  6. {
  7. // pre-Honeycomb has a different way of setting the CheckBox button drawable
  8. resID = Resources.getSystem().getIdentifier("btn_check", "drawable", "android");
  9. }
  10. else
  11. {
  12. // starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable
  13. TypedValue value = new TypedValue();
  14. getApplicationContext().getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);
  15. resID = value.resourceId;
  16. }
  17. return getResources().getDrawable(resID);
  18. }

第二,我创建了一个类“缩放绘制”。请注意,它从标准ScaleDrawable不同。

  1. import android.graphics.drawable.*;
  2. /** The drawable that scales the contained drawable */
  3. public class ScalingDrawable extends LayerDrawable
  4. {
  5. /** X scale */
  6. float scaleX;
  7. /** Y scale */
  8. float scaleY;
  9. ScalingDrawable(Drawable d, float scaleX, float scaleY)
  10. {
  11. super(new Drawable[] { d });
  12. setScale(scaleX, scaleY);
  13. }
  14. ScalingDrawable(Drawable d, float scale)
  15. {
  16. this(d, scale, scale);
  17. }
  18. /** set the scales */
  19. void setScale(float scaleX, float scaleY)
  20. {
  21. this.scaleX = scaleX;
  22. this.scaleY = scaleY;
  23. }
  24. /** set the scale -- proportional scaling */
  25. void setScale(float scale)
  26. {
  27. setScale(scale, scale);
  28. }
  29. // The following is what I wrote this for!
  30. @Override
  31. public int getIntrinsicWidth()
  32. {
  33. return (int)(super.getIntrinsicWidth() * scaleX);
  34. }
  35. @Override
  36. public int getIntrinsicHeight()
  37. {
  38. return (int)(super.getIntrinsicHeight() * scaleY);
  39. }
  40. }

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

  1. import android.graphics.*;
  2. import android.graphics.drawable.Drawable;
  3. import android.widget.*;
  4. /** A check box that resizes itself */
  5. public class WonCheckBox extends CheckBox
  6. {
  7. /** the check image */
  8. private ScalingDrawable checkImg;
  9. /** original height of the check-box image */
  10. private int origHeight;
  11. /** original padding-left */
  12. private int origPadLeft;
  13. /** height set by the user directly */
  14. private float height;
  15. WonCheckBox()
  16. {
  17. super(WonActivity.W.getApplicationContext());
  18. setBackgroundColor(Color.TRANSPARENT);
  19. // get the original drawable and get its height
  20. Drawable origImg = WonActivity.W.getDefaultCheckBoxDrawable();
  21. origHeight = height = origImg.getIntrinsicHeight();
  22. origPadLeft = getPaddingLeft();
  23. // I tried origImg.mutate(), but that fails on Android 2.1 (NullPointerException)
  24. checkImg = new ScalingDrawable(origImg, 1);
  25. setButtonDrawable(checkImg);
  26. }
  27. /** set checkbox height in pixels directly */
  28. public void setHeight(int height)
  29. {
  30. this.height = height;
  31. float scale = (float)height / origHeight;
  32. checkImg.setScale(scale);
  33. // Make sure the text is not overlapping with the image.
  34. // This is unnecessary on Android 4.2.2, but very important on previous versions.
  35. setPadding((int)(scale * origPadLeft), 0, 0, 0);
  36. // call the checkbox's internal setHeight()
  37. // (may be unnecessary in your case)
  38. super.setHeight(height);
  39. }
  40. }