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 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...
随机推荐
- [BZOJ2743] [HEOI2012] 采花 (树状数组)
Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一 ...
- 一个题目涉及到的50个Sql语句
原博客地址 http://blog.csdn.net/maco_wang/article/details/6281484 Student(S#,Sname,Sage,Ssex) 学生表 Course( ...
- 关系型数据库工作原理-客户端连接管理器(翻译自Coding-Geek文章)
本文翻译自Coding-Geek文章:< How does a relational database work>.原文链接:http://coding-geek.com/how-data ...
- python xlsxwriter库生成图表的应用
xlsxwriter可能用过的人并不是很多,不过使用后就会感觉,他的功能让你叹服,除了可以按要求生成你所需要的excel外 还可以加上很形象的各种图,比如柱状图.饼图.折线图等. 请看本人生成的: 这 ...
- 11.python线程
基本概念 1.进程 定义: 进程就是一个程序在一个数据集上的一次动态执行过程. 组成: 进程一般由程序.数据集.进程控制块三部分组成. 程序: 我们编写的程序用来描述进程要完成哪些功能 ...
- 洛谷 P1564 膜拜
题目出处 s[i]表示前i个人对神牛的膜拜情况,如果膜拜神牛甲则s[i]=s[i-1]+1否则s[i]=s[i-1]-1.那么如果|s[i]-s[j]|<=m或者=i-j+1(也就是人数差不超过 ...
- WordPress制作圆形头像友情链接页面的方法
网上看见过很多种友情链接页面,我比较喜欢的是圆形头像的这种,先看看效果吧:传送门 就是这种上面是圆形的友链用户头像,下面是友链用户网站名,然后鼠标移上去头像会旋转,怎么实现这种效果呢?我在网上找了很多 ...
- Windows Server 2019 预览版介绍
在Windows server 2012.Windows server 2016还未完全普及的情况下,昨天Windows Server团队宣布Windows Server 2019将在2018年的下半 ...
- pt工具主从一致性检查并修复以及版本3.0.4的版本缺点
pt-table-checksum和pt-table-sync分别检验master-slave的数据不一致并修复. 1.本次测试环境 [root@172-16-3-190 we_ops_admin]# ...
- Linux shell 基础
目录 一.shell脚本的基本使用 1.语言规范 2.变量 3.重定向(>,>>) 二.运算符和常用判断 1.比较运算符 2.逻辑运算符 3.常用判断 三.程序结构 1.分支(if语 ...