setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
前言:今天看源码看到的setCompoundDrawablesWithIntrinsicBounds方法当时初步理解就是在view的不同方向设置drawable图像,上网上查了查,就发现还有一个
setCompoundDrawables的方法。手工设置文本与图片相对位置时,比如edittext中的设置左边图像或者右边图像一样,这里就来说说他们的区别
他们都是TextView的方法,如果不设置就可以在相应的参数位置上设置为null
1.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
<1>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:组件的高度。
<2>用法
1 mBtn = (Button) findViewById(R.id.id_btn); 2 Drawable icon = this.getResources().getDrawable(R.drawable.ic_launcher); 3 // 必须设置 4 icon.setBounds(1, 1, 100, 100); 5 mBtn.setCompoundDrawables(null, null, icon, null);
效果:
2.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)
<1>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获取。
<2>用法
1 mBtn = (Button) findViewById(R.id.id_btn); 2 Drawable icon = this.getResources().getDrawable(R.drawable.ic_launcher); 3 mBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
效果
转载于:https://www.cnblogs.com/dukc/p/5137194.html
setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别相关推荐
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
手工设置文本与图片相对位置时,常用到如下方法: setCompoundDrawables(left, top, right, bottom) setCompoundDrawablesWithIntri ...
- 经由过程代码设置radiobutton不合方位图标的两种办法
调换radiobutton中的图片在xml中很好设置,但对于初学者如安在代码中设置还是不轻易找的.没办法,经由过程看原版api找到两个办法,setCompoundDrawables和setCompou ...
- setleft android,android TextView的setCompoundDrawables()方法
这个方法可以在TextView的四周加上一个Drawable图标.对于只知道TextView显示文字的,是不是很高大上. setCompoundDrawables(Drawable left, Dra ...
- RPC 笔记(01)— RPC概念、调用流程、RPC 与 Restful API 区别
1. 基本概念 PRC 远程过程调用 Remote Procedure Call,其就是一个节点请求另外一个节点提供的服务.当两个物理分离的子系统需要建立逻辑上的关联时,RPC 是牵线搭桥的常见技术手 ...
- C++ 笔记(28)— C++ 中 NULL和 nullptr 的区别
最近看公司代码的时候发现在判断指针是否为空的时候,有的时候用的是 NULL, 有的时候用的是 nullptr 感觉很奇怪,好奇心驱使我查了下两者的区别,发现还是有很多细节需要学习的. 1. NULL ...
- gcc 和 g++ 的联系和区别,使用 gcc 编译 c++
GCC 编译器已经为我们提供了调用它的接口,对于 C 语言或者 C++ 程序,可以通过执行 gcc 或者 g++ 指令来调用 GCC 编译器. 实际使用中我们更习惯使用 gcc 指令编译 C 语言程序 ...
- Python2 与 Python3 区别
Python2.x 与 Python3.x 区别 1. print 函数 Python2 中 print 是语句(statement),Python3 中 print 则变成了函数.在 Python3 ...
- Docker 入门系列(1)- 初识容器,镜像、容器、仓库的区别
Docker 简介 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发 ...
- HTTP 协议入门 — (TCP/IP协议族、通信传输流、URI 与 URL 的区别、Cookie 状态管理、HTTP 支持的方法、状态码类别、HTTP 首部字段)
TCP/IP协议族 在介绍 HTTP 协议之前,我们先对 TCP/IP 协议族有个大概的了解,TCP/IP 协议从上到下主要分为应用层.传输层.网络层和数据链路层,各层的主要功能如下表所示: 协议层 ...
- python二进制打开(rb)和文本格式打开(r)什么区别?
使用 open() 函数以文本格式打开文件和以二进制格式打开文件,唯一的区别是对文件中换行符的处理不同. 在 Windows 系统中,文件中用 "\r\n" 作为行末标识符(即换行 ...
最新文章
- 机器学习笔记:triplet loss
- Jumping Monkey 并查集,反向思维
- .Net Discovery系列之四 深入理解.Net垃圾收集机制(下)
- [css] css的哪个属性可以把所有元素或其父元素的属性重置呢?
- 复习JavaFile类_递归_综合案例
- JavaWeb笔记:Html总结
- 对汉诺塔递归算法的理解(图解,附完整代码实现)
- 【BZOJ3191】卡牌游戏,概率DP
- 新款iPhone SE预购好于预期,新款iPhone SE Plus可能要因此延迟了
- python导出excel数据表中追加数据_python pandas在已存在的excel中追加数据
- 三大无线技术 —— WiFi、蓝牙、HomeRF(无线网卡、WPAN)
- 与代数式xyz所对应的C语言表达式是,C程序习题集讲解.doc
- linux安装软件命令总结
- 声卡loopback有什么用_sam机架宿主软件无法获取声卡ASIO问题解析
- Word文档怎么添加删除线
- 数码管与74HC573,74HC138电路
- 打造一个基于机器学习的图像解码平台
- c#与汇川机器人通讯_C#与西门子PLC通讯
- 点沙成金:半导体芯片(转载)
- 第一册:lesson thirteen.
热门文章
- Vidmore Player for Mac(多功能蓝光播放器)
- iTerm2 如何设置以单词为单位快速移动光标?
- Mac电脑睡眠之后苹果电脑没有声音的解决办法
- 让MacOS废纸篓中超过30 天的文件自动清除
- JAVA 多用户商城系统b2b2c- 服务消费者(rest+ribbon)
- OkHttpClient源码分析(四)—— CacheInterceptor
- 「LibreOJ#516」DP 一般看规律
- 三分钟带你了解移动通讯办公软件
- Android Messenger 跨进程通信
- test luasql's postgresql driver performance (not better than pgbench)