RadioButton的图标改变大小(TextView也适用)
RadioButton的图标大小并没有相应的布局参数,本文通过自定义属性的方式自定义RadioButton,实现控制图片大小。
- 本文要点:
- 自定义属性的使用。
- 解决RadioButton文字上、下、左、右的图标大小自定义问题。
- 此方法对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 中的图片大小
- 注意:
- <top.toly.www.myqq.view.MyRadioButton 为自定义控件类的全路径名
xmlns:toly="http://schemas.android.com/apk/res-auto" 声明命名空间,其中toly为自定义名称,可替换(需与第3点冒号前名称一致)
toly:rb_width="30dp" toly:rb_height="30dp" 为自定义属性的使用
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也适用)的更多相关文章
- 2018.10.10 MAC 的Launchpad图标改变大小的设置
mac更改launchpad图标大小 设置每列显示的图标数目为8 defaults write com.apple.dock springboard-columns -int 8 设置每行显示的图标数 ...
- QTableView的表格项中加入图标的方法(重载View::mouseMoveEvent,并使用View::setIconSize函数设置图标的大小)
当在使用表格视图的时候,需要在表格每一行前面加入图标,应该怎么做呢?Qt中通过使用MVC的处理方式,很容易做到这一点,具体实现如下: 先贴出图,让大家一睹为快 下面我就来介绍一下,上图的灯泡是怎么实现 ...
- node-webkit学习之【无边框窗口用JS实现拖动改变大小等】
效果如下图 原生的如下图(原生的用在自己的app上面太丑了,并且还带边框,所以重写了左上角的三个功能键) 1.首先了解一下nw底下的package.json 文件 { "name" ...
- 【转】C# 控件的自定义拖动、改变大小方法
在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...
- Qt 无边框窗体改变大小 完美实现(全部自己实现)
近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...
- JS实现漂亮的窗口拖拽效果(可改变大小、最大化、最小化、关闭)
转自<JS实现漂亮的窗口拖拽效果(可改变大小.最大化.最小化.关闭)>:http://www.jb51.net/article/73157.htm 这篇文章主要介绍了JS实现漂亮的窗口 ...
- 黄聪:C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果) (转)
一.各种旋转.改变大小 注意:先要添加画图相关的using引用. //向右旋转图像90°代码如下:private void Form1_Paint(object sender, System.Wind ...
- C# (GDI+相关) 图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果) (转)
C#图像处理 (各种旋转.改变大小.柔化.锐化.雾化.底片.浮雕.黑白.滤镜效果) 一.各种旋转.改变大小 注意:先要添加画图相关的using引用. //向右旋转图像90°代码如下 ...
- Qt:无标题栏无边框程序的拖动和改变大小
From: http://blog.csdn.net/kfbyj/article/details/9284923 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...
随机推荐
- [HDU5765]Bonds
题面 题意 给出一张\(n\)点\(m\)边无向连通图,求每条边出现在多少个割集中. \(n\le20,m\le\frac{n(n-1)}{2}\) sol 所谓割集,就是指把\(n\)个点分成两个集 ...
- 递归方法,查询出树该组织及以下组织的组织ID
-- 查询出该组织下所有组织id的集合 --方法一: public String getAllOrgidsTwo(Integer orgid){ List<Integer> orgids= ...
- angular+ionic+cordova(实战项目开发中,持续更新自己学到的和遇到的)
最近公司开始准备做app了,大佬选择了angular+ionic+corvoda的开发结构,但是对于刚刚才开始对angular才有一点点感觉的我,就像是被一击闷棍敲了,半天没反应过来,emmm,怎么办 ...
- canvas练手项目(二)——各种操作基础
想想应该在canvas上面作画了,那么就不得不提到事件了. (打着canvas的旗号,写着mouse事件.挂羊头卖狗肉!哈哈哈哈哈~) 先来看一看HTML事件属性,我们要用的就是Mouse事件,就先研 ...
- 异步解决方案promise及源码实现
js语言的特性,造就了特别的异步处理方式,我记得以前用的最多的就是回调函数,那个时候写jquery的ajax时候,特别喜欢写这种代码: $.ajax({ method:'get', url:" ...
- python之hasattr()、 getattr() 、setattr() 函数
这三个方法可以实现反射和内省机制,在实际项目中很常用,功能也很强大. [转]http://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, na ...
- POI读写Excel-操作包含合并单元格操作
在上篇博客中写到关于Excel操作解析成相关的类,下面将写入一种Excel对Excel表格读取和写入. 对于Excel表格操作,最重要的是创建workBook.其操作顺序是: 1.获得WorkBook ...
- selenium 断言与验证
断言和验证都是判断结果是否跟预期效果是否一致,不一致的情况下,断言会导致测试用例直接失败,程序不会继续执行:验证的测试用例会继续执行. 断言的4种模式+5种手段: assert 断言失败时,该测试将终 ...
- Java内存区域之程序计数器--《深入理解Java虚拟机》学习笔记及个人理解(一)
Java虚拟机程序计数器 在书上的P39页 程序计数器干嘛的? 有了它,字节码解释器才可以知道下一条要执行的字节码指令是哪个. 无论是取下一条指令还是分支.循环.跳转.中断.线程恢复,都需要这个程序计 ...
- Java 缩写总结
1.JVM:Java Virtual Machine(Java虚拟机)的缩写. 它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. Java语言的一个非常重要的特点就是与 ...