通过代码设置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获取。——笔者翻译 |
- 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 had 意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为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不同方位图标的两种方法的更多相关文章
- Android 通过代码设置radiobutton不同方位图标的两种方法
更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的.没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompound ...
- 华硕笔记本怎么设置u盘启动(两种方法)
华硕笔记本怎么设置u盘启动(两种方法) 华硕笔记本怎么设置u盘启动.我想用U盘安装系统但是 我不知道如何设置U盘启动,那么该如何设置呢?下面和大家分享一下我的经验,希望能够帮到大家.如果你的系统是预装 ...
- 设置div背景透明的两种方法
实现div背景透明的两种方法 1.使用opacity属性 background-color:#000; opacity: 0.5; 这样做可以设置div内部所以区域的透明度,但是也会影响里面的文字,效 ...
- 设置emacs启动窗口的两种方法
1. 设置位置和大小 ;;设置窗口位置为屏库左上角(0,0) (set-frame-position (selected-frame) 0 0) ;;设置宽和高 (set-frame-width (s ...
- CentOS设置服务开机启动的两种方法
一.通过服务的方式设置自启动 1. 在/etc/init.d 下建立相关程序的启动脚本 2. chkconfig --add mysqld(添加服务到chkconfig列表中) chkconfig ...
- idea设置tomcat虚拟路径的两种方法
1.使用tomcat自己的虚拟路径 1.1.在tomcat\config\server.xml中配置 path="/upload" 虚拟路径 E:\photo\upload 图片存 ...
- Vue设置路由跳转的两种方法: <router-link :to="..."> 和router.push(...)
一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...
- Ajax设置自定义请求头的两种方法
用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...
- 48-设置tomcat虚拟路径的两种方法(Eclipse、tomcat、IDEA)
设置tomcat虚拟路径的两种方法(Eclipse.tomcat.IDEA) 三种方式设置虚拟服务器路径如果我们要实现一个上传文件的功能,但是又想要上传的文件不会随着自己web服务器的重启而不能访问了 ...
随机推荐
- hdu 2105
#include <iostream> #include <stdio.h> using namespace std; int main() { double a,b,c,d, ...
- java transient关键字和transaction的区别
transient:表示临时的,不会被持久化,保存进数据库 transaction:表示事务 <div style="background: #fff; color: #0ff;&qu ...
- 一个CSS小测试
<!DOCTYPE html> <html> <head> <style type="text/css"> body{ margin ...
- zepto源码研究 - zepto.js - 5(dom属性管理)
index: $.fn = {...... indexOf: emptyArray.indexOf,} index: function(element){ //这里的$(element)[0]是为了将 ...
- poj1182 并查集
题目连接:http://poj.org/problem?id=1182 基础并查集,需要维护与根节点关系,解析见代码: /* poj 1182 并查集 思路分析:让你分析这些话里面多少假的 只需要用 ...
- CentOS 7 之Cisco Anyconnect Secure Mobility Client
公司使用的是Cisco VPN, 于是准备使用一下.先登录公司的vpn页面,意料之中的失败,所以下载了vpnsetup.sh这个来手动安装. 手动是要用root的,不过由于我是个人学习使用机器,一直用 ...
- jQuery中的$("#my_id").html()中一点要注意的
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXcAAAA3CAIAAAB4jZ1xAAAJdUlEQVR4nO2dPU/rPBTHn2/VoVMrXZ
- jq仿虾米网flash效果
这是很久以前写的一个效果了,之前虾米音乐网首页的一个flash效果,最初觉得这flash效果也可以完全用jq来写,于是空余时间就写了下当作练习吧,现在就拿出来跟大家分享下其中的实现原理! 先上最终效果 ...
- app.config动态修改及读取
1.添加应用程序配置文件 右键点击项目,选择“添加”→“添加新建项”→“添加应用程序配置文件”将其添加到项目中. 2.设置配置文件 <?xmlversion="1.0"enc ...
- C程序设计语言练习题1-20
练习1-20 编写程序detab,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方.假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位.n应该是变量还是符号 ...