当前位置: 首页 > news >正文

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【嵌入式开发板】大家都在玩儿的4412开发板
  • Hadoop2源码分析-MapReduce篇
  • CentOS 7安装后配置笔记
  • Linux常用的基本命令11
  • 40策略时独立需求的冲减
  • js里正则表达式详解
  • mysql表的导入导出
  • autocomplete实现联想输入,自动补全
  • Android+struts2+JSON方式的手机开发(Login)
  • AOP (面向切面编程)
  • CSS背景属性Background详解
  • 第十六条:组合优先于继承
  • 安装readline-6.2.4.1
  • mysql锁机制详解及死锁处理方式
  • 贪心 Codeforces Round #301 (Div. 2) B. School Marks
  • 30秒的PHP代码片段(1)数组 - Array
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Apache的80端口被占用以及访问时报错403
  • Centos6.8 使用rpm安装mysql5.7
  • Cookie 在前端中的实践
  • Debian下无root权限使用Python访问Oracle
  • golang 发送GET和POST示例
  • HTTP中的ETag在移动客户端的应用
  • Java反射-动态类加载和重新加载
  • js数组之filter
  • Python_OOP
  • rc-form之最单纯情况
  • SpringBoot 实战 (三) | 配置文件详解
  • use Google search engine
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 浮动相关
  • 回顾2016
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 免费小说阅读小程序
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 强力优化Rancher k8s中国区的使用体验
  • 使用SAX解析XML
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #pragma once
  • #QT项目实战(天气预报)
  • #前后端分离# 头条发布系统
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • ${factoryList }后面有空格不影响
  • $refs 、$nextTic、动态组件、name的使用
  • (02)vite环境变量配置
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (第61天)多租户架构(CDB/PDB)
  • (多级缓存)多级缓存
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)计算机毕业设计SSM智慧停车系统
  • (每日持续更新)jdk api之FileFilter基础、应用、实战