前几天写demo,底部是一个RadioGroup+RadioButton的导航按钮,文字在下面,图片在上面。随便导入了几张图片,本来图片很大,想着xml里面能不能设置,以前项目中UI给的图刚好合适,也就没有注意,结果发现xml里面无法设置图片的大小,文字直接被挤掉了。很郁闷啊,搞了半天,查源码,百度,最后发现,xml里面根本不能修改图片的大小。倒是总结了两种可行的方法,供大家参考。
第一种:在代码中修改。
第一步:设置布局,找到RadioButton控件
setContentView(R.layout.activity_main);
RadioButton  rb=(RadioButton)findViewById(R.id.rb);
第二步:找到你要设置大小的图片资源
Drawable drawable = getResources().getDrawable(R.drawable.img_rb);//找到图片
drawable.setBounds(0, 0, 60, 60);//最关键的一步,给图片设置大小,4个参数分别是左上右下,我看好多人对这个左上右下不理解,看下源码

rb.setCompoundDrawables(null, drawable, null, null);

++++++++++++++++++++++源码++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Specify a bounding rectangle for the Drawable. This is where the drawable
* will draw when its draw() method is called.
* 这句话的意思是说:指定一个可拉边界的矩形。
* ok,就是说给图片一个矩形,起始大小都为0,我们分别指定图片右边、图片下边距离矩形左边和上边的大小,这不就是图片的大小了嘛
*/
public void setBounds(int left, int top, int right, int bottom) {
Rect oldBounds = mBounds;
if (oldBounds == ZERO_BOUNDS_RECT) {
oldBounds = mBounds = new Rect();
}
if (oldBounds.left != left || oldBounds.top != top ||
oldBounds.right != right || oldBounds.bottom != bottom) {
if (!oldBounds.isEmpty()) {
// first invalidate the previous bounds
invalidateSelf();
}
mBounds.set(left, top, right, bottom);
onBoundsChange(mBounds);
}
}
++++++++++++++++++++++++源码++++++++++++++++++++++++++++++++++++++++++++++++
第二种:自定义一个MyRadioButton。原理跟第一种是一样的,直接上代码吧。
第一步:在attrs文件夹下面定义一个图片引用的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyRadioButton">
<attr name="drawableTop" format="reference" />
</declare-styleable>
</resources>
第二步:定义一个MyRadioButton类,继承原生的RadioButton
public class MyRadioButton extends RadioButton {
private Drawable drawable;
public MyRadioButton(Context context) {
super(context);
}
public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);//获取我们定义的属性
drawable = typedArray.getDrawable(R.styleable.MyRadioButton_drawableTop);
drawable.setBounds(0, 0, 60, 60);
setCompoundDrawables(null, drawable, null, null);
}
}
第三步:xml中使用它:
<com.myapp.view.MyRadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="哈哈1"
attr:drawableTop="@drawable/单个图片或者选择器图片" />
<com.myapp.view.MyRadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="哈哈2"
attr:drawableTop="@drawable/-----" />
<com.myapp.view.MyRadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="哈哈3"
attr:drawableTop="@drawable/----" />
运行:完美!
需要主意的是:要是在RadioGroup中默认选中第几个,但是点击后不互斥,这是因为没有给RadioButton写id。

RadioButton设置图片大小相关推荐

  1. word 批量设置图片大小

    word批量修改图片大小--固定长宽篇 方法一: 这部分要说的是把word中的所有图片修改成固定的并且相同的长和宽! 1.打开word,工具-宏-宏(或者直接按Alt+F8)进入宏的 界面,如下面所示 ...

  2. C# 操作word 指定书签位置插入图片并设置图片大小和位置

    object Nothing = System.Reflection.Missing.Value;         //创建一个名为wordApp的组件对象         Application w ...

  3. java 在线给word 文件插入图片,并设置图片大小

    一.前言: 有时我们想在一篇word文档中的指定位置添加一张服务器上的图片,并且并且设置图片大小.这个对文档的操作一定是需要第三方api的.市面上对文档处理的插件有POI以及openoffice等等, ...

  4. 视频直播源码,插入图片、删除图片、设置图片大小、提取图片

    视频直播源码,插入图片.删除图片.设置图片大小.提取图片 1.插入图片 Document对象有一个add_paragraph()方法插入图片,只需要传入路径或者字节流即可,实际上它也是调用段落的Run ...

  5. andriod studio怎么设置图片大小_Word图片大小总是对不齐,如何统一图片的大小位置,看一眼就会!...

    还在为图片大小位置不统一而发愁吗,这样做出来的word文档不美观图片看起来也是大小不一很不协调.今天就教大家几个简单的处理图片的办法,快来学习吧! 1. 修改图片默认环绕方式 大家在word中插入图片 ...

  6. markdown如何设置图片大小_不会吧,还不会用markdown排版吗

    请使用 Chrome 浏览器. 请阅读下方文本熟悉工具使用方法,本文可直接拷贝到微信中预览. 1 Markdown Nice 简介 支持自定义样式的 Markdown 编辑器 支持微信公众号.知乎和稀 ...

  7. RadioButton 修改图片大小的方式

    在开发中,使用RadioButton做首页底部的按钮,那么如果图片过大就会很难看 //定义底部标签图片大小 Drawable drawableFirst = getResources().getDra ...

  8. matplotlib中文显示以及设置图片大小

    1.找到字体路径 mac字体的路径一般都是在:/System/Library/Fonts 下 如果不放心可在:访达>字体册>简体中文>在访达中显示>显示简介>位置,在将字 ...

  9. 【CSDN】如何设置图片大小(等比例)

    方法很简单,一学就会 我有一张原图,简直太大了 正常使用markdown的语法是这样,这样生成的图片,我们可能不太满意,要么大要么小 我们在末尾右半括号的前面输入空格,再输入等于号,然后输入你要设置的 ...

  10. matplotlib折线图(设置图片大小和图片保存)

    代码示例: from matplotlib import pyplot as plt x = range(1,10) #x轴的位置 y = [6,7,12,12,15,17,15,20,18] #y轴 ...

最新文章

  1. Linux环境搭建 手把手教你配置Linux虚拟机
  2. [SoapUI] 在Test Step 下加Script Assertion,用 messageExchange 获取当前步骤的response content...
  3. linux安装mysql5.6
  4. android背景不填充,(Android Studio)应用程序背景图像不填充屏幕
  5. 可怕的春运,烦人的火车票!
  6. 欧奈尔4个经典形态_股票K线图基础知识:图解4大经典K线组合形态
  7. dell笔记本c语言系统,戴尔dell笔记本电脑U盘重装系统win7教程图解
  8. rust自我解脱_自我解脱
  9. datagrid---formatter方法
  10. 【思科模拟器基本使用】
  11. 我的口琴之路(附c调简谱)--------一个命中注定音乐巅峰是小星星的男人
  12. pyalgotrade源码分析4--PyAlgoTrade统计指标
  13. RK3588 烧写固件
  14. ins是什么与Instagram有什么区别
  15. 游戏测试和普通测试有什么区别?
  16. 关于单片机对三极管B值测量的硬件电路和软件思路分享
  17. kickstart无人值守
  18. 怎么解除Word文档限制编辑?比较方便的两种办法
  19. 玩转电源设计,8个优选逆变电源参考方案大合辑
  20. Office for Mac 2019 v16.40 版发布下载

热门文章

  1. OCR怎么能离开扫描仪呢?
  2. 计算机兼容性测试怎么做,如何进行兼容性测试
  3. 计算机怎样更新目录,怎么在word中设置自动更新目录功能
  4. 用HTml+csss实现优惠券
  5. 输入日期得出星座c语言,根据日期计算星座
  6. 统计矩形中的正方形和长方形
  7. 怎样启动本地mysql服务_启动本地mysql服务
  8. word表格转图片线条不会缺失方法
  9. 一定是h的方式不对阅读_41章_[黑篮]一定是H的方式不对!_七零小说网
  10. 如何测试短信验证码平台的安全和实用性?