Android 通过代码设置radiobutton不同方位图标的两种方法

更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的。没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds。

下面交给大家方法。

第一个方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables‘ bounds will be set to their intrinsic bounds.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。——笔者翻译

button = (RadioButton) group.getChildAt(i);
Resources res = TabTest.this.getResources();
Drawable myImage = res.getDrawable(R.drawable.home);
button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);

第二种方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:


Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。——笔者翻译

这下就明白了,这个方法要先给Drawable设置setBounds(x,y,width,height);

x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度。

如代码:

     Resources res = TabTest.this.getResources();
     Drawable myImage = res.getDrawable(R.drawable.home);
     myImage.setBounds(1, 1, 100, 100);
    button.setCompoundDrawables(null, myImage, null, null);

只要调整好宽和高。效果也是一样的。这个方法的好处就是不按比例,宽高可以打破原有的大小及比例!如图,我调的y轴有点不对齐。

总结:radiobutton设置不同方位的图标的方法有以上两种,如果想手动设置大小的话就要用setCompoundDrawables,事先要给Drawable设置setBounds。

如果按照原有比例大小显示图片就使用setCompoundDrawablesWithIntrinsicBounds

时间: 2024-07-29 15:52:19

Android 通过代码设置radiobutton不同方位图标的两种方法的相关文章

通过代码设置radiobutton不同方位图标的两种方法

更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds. 下面交给大家方法. 第一个方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) ap

Android在布局中动态添加view的两种方法

一.说明 添加视图文件的时候有两种方式:1.通过在xml文件定义layout:2.java代码编写 二.前言说明 1.构造xml文件 2.LayoutInflater 提到addview,首先要了解一下LayoutInflater类.这个类最主要的功能就是实现将xml表述的layout转化为View的功能.为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutI

设置数据库兼容级别的两种方法以及区别

转:http://blog.csdn.net/htl258/article/details/5696325 --设置数据库兼容级别的两种方法 --以设置兼容SQL Serve 2005 为例 --法一: ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = 90 GO --法二: EXEC sp_dbcmptlevel database_name,90 GO database_name 要修改为数据库的名称. 各参数值对应的数据库版本: 8

华硕笔记本怎么设置u盘启动(两种方法)

华硕笔记本怎么设置u盘启动(两种方法) 华硕笔记本怎么设置u盘启动.我想用U盘安装系统但是 我不知道如何设置U盘启动,那么该如何设置呢?下面和大家分享一下我的经验,希望能够帮到大家.如果你的系统是预装win8的系统,那么如果你要想安装win7系统,需要你在BIOS中有相关的设置,你可以看我之前的经验,建议在百度上搜索---华硕笔记本预装win8改win7如何在bios中设置 工具/原料 华硕笔记本 U盘启动 方法一.bios设置开机启动 1 开机长按F2键,进入bios. 2 我们左右移动找到"

Android中使用Gson解析JSON数据的两种方法

Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率. 从结构上看,所有的数据(data)最终都可以分解成三种类型: 第一种类型是标量(scalar),也就是一个单独的字符串(string)或数字(numbers),比如"北京"这个单独的词. 第二种类型是序列(sequence),也就是若干个相关的数据按照一定顺序并列在一起,又叫做数组

代码设置RadioButton的高端技巧

不知道怎么起标题,就这样了. 目前主要讲两个方面内容: 代码方式 设置RadioButton的 android:button . android:background 等属性为 @null : 代码方式 通过布局模板动态创建固定参数的RadioButton等控件对象 1.代码设置@null // 这里不能用null,必需采用以下方式设置 radioButton.setButtonDrawable(getResources().getDrawable(android.R.color.transpa

Android中如何设置RadioButton在文字的右边,图标在左边

from:http://blog.csdn.net/sunnyfans/article/details/7901592?utm_source=tuicool&utm_medium=referral Android中如何设置RadioButton在文字的右边,图标在左边??? 解决方法  :第一步:android:button="@null"这条语句将原来系统的RadioButton图标给隐藏起来.第二步: android:drawableRight="@android

Android 更新UI的两种方法——handler和runOnUiThread() - $firecat的代码足迹$ - 博客频道 - CSDN.NET

文章来源:http://www.2cto.com/kf/201302/190591.html Android 更新UI的两种方法——handler和runOnUiThread() 在Android开发过程中,常需要更新界面的UI.而更新UI是要主线程来更新的,即UI线程更新.如果在主线线程之外的线程中直接更新页面显示常会报错.抛出异常:android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread th

QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)

QT中获取选中的radioButton的两种方法 QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast<QRadioButton*>(ui->BG->checkedButton()); 2 QString name = pbtn->objectName(); 3 if(!QString::compare(name, "rad