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 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...
随机推荐
- 【POJ3207】Ikki's Story IV - Panda's Trick
POJ 3207 Ikki's Story IV - Panda's Trick liympanda, one of Ikki's friend, likes playing games with I ...
- [BZOJ1001] [Beijing2006] 狼抓兔子 (最短路)
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...
- [BZOJ1087] [SCOI2005] 互不侵犯King (状压dp)
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- web安全之SQL注入
一.sql注入是一种将sql代码添加到输入参数中 传递到sql服务器解析并执行得一种攻击手法 例: $sql = "SELECT * FROM goods WHERE Id = 1" ...
- FTP环境搭建及客户代码调用公共方法封装
一.背景 大型系统架构往往被分解为多个独立可运行的组件, 以满足性能.可靠性.可扩展性的需求.多个组件间的数据交互往往采用两种方式:小量数据通过Sock函数.RMI.WebService等接口方式传递 ...
- c#事件的应用
我已经多次使用c#事件了.举个最近的例子:我用vsto做的一个word插件.这个插件分为两层,业务逻辑层和word外接程序.有这么一个场景,我的ribbon中的一个label是动态显示的.它会随着wo ...
- 20.DOM
定义 文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口. 查找元素 1.直接查找 document.getElementById 根据ID获取一个标签 ...
- 【Unity3D与23种设计模式】中介者模式(Mediator)
GoF中定义: 定义一个接口来封装一群对象的互动行为 中介者通过移除对象之间的引用 以减少他们之间的耦合度 并且能改变它们之间的互动独立性 游戏做的越大,系统划分的也就越多 如事件系统,关卡系统,信息 ...
- Linux Centos 下安装软件 三种方式(转)
Linux学习的路还很远呢,各位码农,新年快乐哈! 1)一种是软件的源代码,您需要自己动手编译它.这种软件安装包通常是用gzip压缩过的tar包(后缀为.tar.gz). 2)另一种是软件的可执行程序 ...
- Linux增加LV(逻辑卷)容量
Linux增加LV(逻辑卷)容量 2017-09-29-17:34:13 个人原创博客,转载请注明出处. 查看逻辑卷的相关命令: lvs vgs 命令: [root@arch ~]# vgs VG # ...