首页 > 其他分享 >Android版本:如何更改复选框的大小

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

时间:2023-04-18 23:55:45浏览次数:53  
标签:scale 更改 float scaleX height drawable Android 复选框 android

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

标签:scale,更改,float,scaleX,height,drawable,Android,复选框,android
From: https://www.cnblogs.com/zbw-m/p/17331747.html

相关文章

  • MAUI Blazor实战 - Android监听返回键
    MAUIBlazor实战-Android监听返回键@目录MAUIBlazor实战-Android监听返回键前言一、常规方法无效?二、使用DispatchKeyEvent总结前言我们在MAUIBlazor项目中有时需要监听Android返回键,例如防止多次点击后退出应用、防止退回到特定页面、以及特定页面禁用退回功能等。一......
  • MASA MAUI Plugin 集成个推,实现本地消息推送(六)【Android】篇
    MASAMAUIPlugin(六)集成个推,实现本地消息推送[Android]篇背景MAUI的出现,赋予了广大Net开发者开发多平台应用的能力,MAUI是Xamarin.Forms演变而来,但是相比Xamarin性能更好,可扩展性更强,结构更简单。但是MAUI对于平台相关的实现并不完整。所以MASA团队开展了一个实验性项目,意在对......
  • MASA MAUI Plugin (七)应用通知角标Android+iOS
    MASAMAUIPlugin(七)应用通知角标(小红点)Android+iOS背景MAUI的出现,赋予了广大Net开发者开发多平台应用的能力,MAUI是Xamarin.Forms演变而来,但是相比Xamarin性能更好,可扩展性更强,结构更简单。但是MAUI对于平台相关的实现并不完整。所以MASA团队开展了一个实验性项目,意在对微软MAU......
  • MASA MAUI Plugin (九)Android相册多选照片(使用Android Jetpack套件库)
    MASAMAUIPlugin(九)Android相册多选照片(使用AndroidJetpack套件库)背景MAUI的出现,赋予了广大Net开发者开发多平台应用的能力,MAUI是Xamarin.Forms演变而来,但是相比Xamarin性能更好,可扩展性更强,结构更简单。但是MAUI对于平台相关的实现并不完整。所以MASA团队开展了一个实验性项......
  • MASA MAUI Plugin (八)Android相册多选照片(Intent 方式)
    MASAMAUIPlugin(八)Android相册多选照片(Intent方式)背景MAUI的出现,赋予了广大Net开发者开发多平台应用的能力,MAUI是Xamarin.Forms演变而来,但是相比Xamarin性能更好,可扩展性更强,结构更简单。但是MAUI对于平台相关的实现并不完整。所以MASA团队开展了一个实验性项目,意在对微软MA......
  • XAPMM上更改了root密码,怎么更新phpMyAdmin的配置文件
    要更新phpMyAdmin的配置文件以反映在XAPMM上更改的root密码,请按照以下步骤操作:1.打开phpMyAdmin的配置文件:在XAPMM中,phpMyAdmin的配置文件通常位于D:\xampp\phpMyAdmin2.在配置文件中,找到以下行:$cfg['Servers'][$i]['user']='root';$cfg['Servers'][$i]['password']......
  • Android之AppWidget 开发浅析
    什么是AppWidgetAppWidget即桌面小部件,也叫桌面控件,就是能直接显示在Android系统桌面上的小程序,先看图:图中我用黄色箭头指示的即为AppWidget,一些用户使用比较频繁的程序,可以做成AppWidget,这样能方便地使用。典型的程序有时钟、天气、音乐播放......
  • Android 自定义View 之 圆环进度条
    圆环进度条前言正文一、XML样式二、构造方法三、测量四、绘制①绘制进度条背景②绘制进度③绘制文字五、API方法六、使用七、源码前言  很多时候我们会使用进度条,而Android默认的进度条是长条的,从左至右。而在日常开发中,有时候UI为了让页面更美观,就需要用到圆环进度条,那么本文......
  • adb(Android Debug Bridge)安装使用教程
    参考:https://www.cnblogs.com/lsdb/p/9438215.html一、说明adb的db是debugbridge而不是和gdb一样指debug,这意思是说adb不能像gdb那样能一步步调试代码,但可以启到一些类似调试的功能。下面就针对这些功能进行介绍,本文根据官方文档“http://adbshell.com/commands”整理而成。......
  • Android生命周期继续踩坑
    android.overridePathCheck=true“覆盖路径检查”as出现问题connecttimeout 在多个窗口中使用Logcat标签页可帮助您在不同的设备或查询之间轻松切换。您可以点击 NewTab 图标  创建多个Logcat标签页。右键点击标签页可对其重命名和重新排列。此外,您还可以在......