更换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获取。——笔者翻译

1                 button = (RadioButton) group.getChildAt(i);
2 Resources res = TabTest.this.getResources();
3 Drawable myImage = res.getDrawable(R.drawable.home);
4 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 hadsetBounds(Rect) called.

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

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

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

如代码:

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

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

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

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

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

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

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

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

    华硕笔记本怎么设置u盘启动(两种方法) 华硕笔记本怎么设置u盘启动.我想用U盘安装系统但是 我不知道如何设置U盘启动,那么该如何设置呢?下面和大家分享一下我的经验,希望能够帮到大家.如果你的系统是预装 ...

  3. 设置div背景透明的两种方法

    实现div背景透明的两种方法 1.使用opacity属性 background-color:#000; opacity: 0.5; 这样做可以设置div内部所以区域的透明度,但是也会影响里面的文字,效 ...

  4. 设置emacs启动窗口的两种方法

    1. 设置位置和大小 ;;设置窗口位置为屏库左上角(0,0) (set-frame-position (selected-frame) 0 0) ;;设置宽和高 (set-frame-width (s ...

  5. CentOS设置服务开机启动的两种方法

    一.通过服务的方式设置自启动 1.  在/etc/init.d 下建立相关程序的启动脚本 2.  chkconfig --add mysqld(添加服务到chkconfig列表中) chkconfig ...

  6. idea设置tomcat虚拟路径的两种方法

    1.使用tomcat自己的虚拟路径 1.1.在tomcat\config\server.xml中配置 path="/upload" 虚拟路径 E:\photo\upload 图片存 ...

  7. Vue设置路由跳转的两种方法: <router-link :to="..."> 和router.push(...)

    一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...

  8. Ajax设置自定义请求头的两种方法

    用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...

  9. 48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)

    设置tomcat虚拟路径的两种方法(Eclipse.tomcat.IDEA) 三种方式设置虚拟服务器路径如果我们要实现一个上传文件的功能,但是又想要上传的文件不会随着自己web服务器的重启而不能访问了 ...

随机推荐

  1. svg转换工具

    package com.rubekid.springmvc.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...

  2. JS判断图片是否加载完成三种方式

    1.img的complete属性 轮询不断监测img的complete属性,如果为true则表明图片已经加载完毕,停止轮询.该属性所有浏览器都支持. <p id="p1"&g ...

  3. linux下软件安装与卸载

    linux上软件二进制安装主要分为:rpm手动安装和yum在线安装(其所安装的都为rpm二进制包). 关于rpm手动安装,学习后面内容前需分清如下内容: 包全名 : 操作的包是没有安装的软件包时,使用 ...

  4. 武汉科技大学ACM:1009: 华科版C语言程序设计教程(第二版)习题5.12

    Problem Description 这天老师照例给小豪出了一道题目:老师给小豪一个字符串,让小豪将该字符串逆序输出. Input 第一行包括一个T,表示有多少组测试数据: 接下来T行,每行包括一个 ...

  5. SQL Server 2008 查询所有用户表

    SQL Server 2008 查询所有用户表的T-SQL语句是: SELECT * FROM sysobjects WHERE [xtype] = 'U' 或者是: SELECT * FROM sy ...

  6. IE9的BUG?jQuery的BUG?

    本文转载自http://big-student.iteye.com/blog/1898213 在IE9和IE10中,当对一个html的样式初始了一个很大的left或者top时,使用jQuery的off ...

  7. jquery.validation.js的自定义方法

    jQuery.validator.addMethod(name,method[,message]) name方法名 method:function(value,element,params)//val ...

  8. PHPCMS 标签与解析小记_Jason

    Content模块下的标签解析:phpcms\modules\content\classes\content_tag.class.php 推荐位:public function position

  9. javascript 网页图标音乐切换

    图片名称 sprite.zip <!doctype html> <html> <head> </head> <style> .css{ po ...

  10. CMD 下 进入 ORACLE

    .sqlplus /nolog .connect sys/orcl@ORCL as sysdba .select sysdate from dual exit;