RadioButton的图标大小并没有相应的布局参数,本文通过自定义属性的方式自定义RadioButton,实现控制图片大小。

  • 本文要点:
  1. 自定义属性的使用。
  2. 解决RadioButton文字上、下、左、右的图标大小自定义问题。
  3. 此方法对TextView内的图标也适用。
  • 问题如下:

  • 解决方法:

1.values/attrs.xml 文件中:自定义rb_width  和  rb_height  两个属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyRadioButton">
<attr name="rb_width" format="dimension"/>
<attr name="rb_height" format="dimension"/>
</declare-styleable>
</resources>

2.自定义RadioButton

 package top.toly.www.myqq.view;

 import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton; import top.toly.www.myqq.R;
import utils.shortUtils.Change; /**
* 作者:张风捷特烈
* 时间:2018/3/28:6:30
* 邮箱:1981462002@qq.com
* 说明:自定义RadioButton 属性:rb_width rb_height
*/
public class MyRadioButton extends RadioButton { private float mImg_width;
private float mImg_height; public MyRadioButton(Context context) {
super(context);
} public MyRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
mImg_width = t.getDimension(R.styleable.MyRadioButton_rb_width, Change.dp2px());
mImg_height = t.getDimension(R.styleable.MyRadioButton_rb_height, Change.dp2px());
t.recycle();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//让RadioButton的图标可调大小 属性:
Drawable drawableLeft = this.getCompoundDrawables()[];//获得文字左侧图片
Drawable drawableTop = this.getCompoundDrawables()[];//获得文字顶部图片
Drawable drawableRight = this.getCompoundDrawables()[];//获得文字右侧图片
Drawable drawableBottom = this.getCompoundDrawables()[];//获得文字底部图片
if (drawableLeft != null) {
drawableLeft.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(drawableLeft, null, null, null);
}
if (drawableRight != null) {
drawableRight.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, null, drawableRight, null);
}
if (drawableTop != null) {
drawableTop.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, drawableTop, null, null);
}
if (drawableBottom != null) {
drawableBottom.setBounds(, , (int) mImg_width, (int) mImg_height);
this.setCompoundDrawables(null, null, null, drawableBottom);
}
}
}

3.使用属性控制RadioButton 中的图片大小

  • 注意:
  1. <top.toly.www.myqq.view.MyRadioButton 为自定义控件类的全路径名
  2.  xmlns:toly="http://schemas.android.com/apk/res-auto"  声明命名空间,其中toly为自定义名称,可替换(需与第3点冒号前名称一致)
  3.  toly:rb_width="30dp"  toly:rb_height="30dp" 为自定义属性的使用
  4.  android:drawableTop="@drawable/tab_msg_selector" 为指定的图片资源
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toly="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/qq_title_text"> <RadioGroup
android:id="@+id/rg_btns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_msg"
android:checked="false"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:button="@null"
android:gravity="center"
android:drawableTop="@drawable/tab_msg_selector"
android:text="消息"/> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_contact"
android:background="@android:color/transparent"
android:checked="false"
android:button="@null"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:gravity="center"
android:drawableTop="@drawable/tab_contact_selector"
android:text="联系人"/> <top.toly.www.myqq.view.MyRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:id="@+id/rb_act"
android:button="@null"
toly:rb_width="30dp"
toly:rb_height="30dp"
android:gravity="center"
android:background="@android:color/transparent"
android:drawableTop="@drawable/tab_act_selector"
android:text="动态"
/>
</RadioGroup>
</RelativeLayout>

4.结果图:

RadioButton的图标改变大小(TextView也适用)的更多相关文章

  1. 2018.10.10 MAC 的Launchpad图标改变大小的设置

    mac更改launchpad图标大小 设置每列显示的图标数目为8 defaults write com.apple.dock springboard-columns -int 8 设置每行显示的图标数 ...

  2. QTableView的表格项中加入图标的方法(重载View::mouseMoveEvent,并使用View::setIconSize函数设置图标的大小)

    当在使用表格视图的时候,需要在表格每一行前面加入图标,应该怎么做呢?Qt中通过使用MVC的处理方式,很容易做到这一点,具体实现如下: 先贴出图,让大家一睹为快 下面我就来介绍一下,上图的灯泡是怎么实现 ...

  3. node-webkit学习之【无边框窗口用JS实现拖动改变大小等】

    效果如下图 原生的如下图(原生的用在自己的app上面太丑了,并且还带边框,所以重写了左上角的三个功能键) 1.首先了解一下nw底下的package.json 文件 { "name" ...

  4. 【转】C# 控件的自定义拖动、改变大小方法

    在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...

  5. Qt 无边框窗体改变大小 完美实现(全部自己实现)

    近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...

  6. JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)

    转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm   这篇文章主要介绍了JS实现漂亮的窗口 ...

  7. 黄聪:C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果) (转)

    一.各种旋转.改变大小 注意:先要添加画图相关的using引用. //向右旋转图像90°代码如下:private void Form1_Paint(object sender, System.Wind ...

  8. C# (GDI+相关) 图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果) (转)

    C#图像处理   (各种旋转.改变大小.柔化.锐化.雾化.底片.浮雕.黑白.滤镜效果)     一.各种旋转.改变大小   注意:先要添加画图相关的using引用.   //向右旋转图像90°代码如下 ...

  9. Qt:无标题栏无边框程序的拖动和改变大小

    From: http://blog.csdn.net/kfbyj/article/details/9284923 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...

随机推荐

  1. POJ1741

    树分治,每次把每个点的deep取出,排序...尺取法... # include <stdio.h> # include <stdlib.h> # include <ios ...

  2. 含隐变量模型求解——EM算法

    1 EM算法的引入1.1 EM算法1.2 EM算法的导出2 EM算法的收敛性3EM算法在高斯混合模型的应用3.1 高斯混合模型Gaussian misture model3.2 GMM中参数估计的EM ...

  3. Django入门-基本数据库API

    # 现在系统里还没有 Question 对象 >>> Question.objects.all() <QuerySet []> # 创建新 Question # 在 se ...

  4. webstorm修改工具栏主题

    ** 操作如图所示 **

  5. c++标准头文件

    C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...

  6. 【Unity与23种设计模式】适配器模式(Adapter)

    GoF中定义: "将一个类的接口转换成为客户端期待的类接口.适配器模式让原本接口不兼容的类能一起合作." 适配器模式与装饰模式有一定的相似之处 两者都是在着手解决C#不能多继承的问 ...

  7. MySQL DBA教程全套视频资料

    MySQL基础入门.MySQL多实例安装与企业应用场景.MySQL应用管理及进阶实战操作.MySQL乱码问题及字符集实战.MySQL备份-增量备份及数据恢复基础实战.MySQL主从复制原理及实战部署. ...

  8. wpf动态增加删除控件

    我在xaml中定义了一个名字为morepictureWrapPan为WrapPanel,然后将控件添加在此WrapPanel中.由于要实现控件的删除功能,所以增加的textbox和button的名字都 ...

  9. 我的linux学习之路——(一)

    prompt:命令提示符 命令: command options...... arguments...... 选项: 短选项 长选项 带参数的选项 参数: list----ls 列出,列表 列出制定路 ...

  10. Mycat 分片规则详解--自然月分片

    实现方式:按照月份列分片,每个自然月一个分片 优点:使数据按照每月来进行分时存储 缺点:由于数据是连续的,所以该方案不能有效的利用资源 配置示例: <tableRule name="s ...