更换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

最新文章

  1. 自增序号,而且默认变量就是$i,也就是说在你的volist标签之内,可以直接使用$i
  2. javascript序列化和反序列化
  3. 如何快速把hdfs数据动态导入到hive表
  4. JAVA 设计模式 策略模式
  5. NGUI Camera's raycast hit through the UI Layer issue
  6. Hbase&Hadoop常用命令
  7. hibernate一对多映射实现
  8. iOS 蒙板,图片叠加显示漏空部分
  9. UVA 11754 - Code Feat(数论)
  10. spring Annotation 组分注塑
  11. UVALive - 3263 That Nice Euler Circuit (几何)
  12. cudaMemcpy与cudaMemcpyAsync的区别
  13. HashMap源码解读(jdk1.8)
  14. android studio 编辑markdown文件
  15. ERP项目实施记录02
  16. go中的map[Interface{}]Interface{}理解
  17. Hibernate+struct web项目问题总结
  18. java工具包一:日期处理
  19. ThinkPHP_5对数据库的CURL操作
  20. JS的__proto__与prototype

热门文章

  1. javascript基础之自执行函数
  2. [JavaScript]plupload多图片上传图片
  3. Android开发手记(14) 使用MediaPlayer播放mp3
  4. sql - 修改结构
  5. CUICatalog: Invalid asset name supplied: (null)
  6. 武汉科技大学ACM:1010: 零起点学算法27——判断是否直角三角形
  7. python的hashlib模块
  8. Java学习笔记——SequenceInputStream类合并文件的综合举例分析
  9. javascript控制图片等比例缩放
  10. Windows10 删除已经保存的WIFI热点