Android 动态设置TextView的drawableLeft等属性
首先,我们在开发过程中,会经常使用到android:drawableLeft="@drawable/ic_launcher"这些类似的属性:
关于这些属性的意思,无非是在你的textView文本的上下左右处添加一个图片。比如下面这么一段代码:
<TextView
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="4dp"
/>
它设置了在文本的左边,显示一个小图标,效果如下:
而在一些情况下,我们需要在动态在代码中设置文本周围的图标,那该如何呢,首先,我们看下TextView提供了哪些方法:
乍眼看去,挺多方法的,好,我们主要介绍setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds。
手工设置文本与图片相对位置时,常用到如下方法:
setCompoundDrawables(left, top, right, bottom)及setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom),它们的意思是设置Drawable显示在text的左、上、右、下位置。
但是两者有些区别:
setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
所以才有The Drawables must already have had setBounds(Rect) called,即使用之前必须使用Drawable.setBounds设置Drawable的长宽。
而setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
所以才有The Drawables' bounds will be set to their intrinsic bounds.即通过getIntrinsicWidth()与getIntrinsicHeight()获得。
一般,建议使用setCompoundDrawablesWithIntrinsicBounds,这样你即无需设置Drawables的bounds了。
看下代码:
TextView textDrawable = (TextView) findViewById(R.id.text_drawable);
R.drawable.ic_launcher);
textDrawable.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
null, null, null);
textDrawable.setCompoundDrawablePadding(4);
效果和以上直接通过android:drawableLeft一样!
Android 动态设置TextView的drawableLeft等属性的更多相关文章
- android 动态设置TextView值,例:金额添加
一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...
- android 动态设置TextView值,例:金额增加
一说到动态递增设置TextView值,很多人应该马上就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值但是效率不咋 ...
- Android中设置TextView的颜色setTextColor
tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...
- 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...
- 【转】Android中设置TextView的颜色setTextColor
原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...
- [转]Android中设置TextView的颜色setTextColor
[转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...
- Android如何设置TextView的行间距、行高。
Android系统中TextView默认行间距比较窄,不美观. 我们可以设置每行的行间距,可以通过属性android:lineSpacingExtra或android:lineSpa ...
- C#动态设置匿名类型对象的属性
用C#写WPF程序, 实现功能的过程中碰到一个需求: 动态设置对象的属性,属性名称是未知的,在运行时才能确定. 本来这种需求可以用 Dictionary<string, object> 实 ...
- Android 动态设置控件获取焦点
之前写过一篇博客,简单的介绍了Android 隐藏EditText的焦点,之所以要隐藏EditText的焦点,是因为当应用在第一次进入某个Activity时,由于该页面中的EditText获取了焦点, ...
随机推荐
- 线程安全(中)--彻底搞懂synchronized(从偏向锁到重量级锁)
接触过线程安全的同学想必都使用过synchronized这个关键字,在java同步代码快中,synchronized的使用方式无非有两个: 通过对一个对象进行加锁来实现同步,如下面代码. synchr ...
- ELK-Logstash采集日志和输送日志流程测试
讲解Logstash采集日志和输送日志流程测试,包括input,filter和output元素的测试 配置一:从elasticsearch日志文件读取日志信息,输送到控制台 $ cd /home/es ...
- 【ASP.NET Core快速入门】(十二)JWT 设计解析及定制
前言 上一节我们讲述的书如何使用jwt token,而且上一节的token是要加Authorization:bearer XXXXXXXXXXXX才能访问. 这一节我们来研究如何自定义类似jwt的to ...
- leetcode — distinct-subsequences
import java.util.Arrays; /** * * Source : https://oj.leetcode.com/problems/distinct-subsequences/ * ...
- 痞子衡嵌入式:第一本Git命令教程(2)- 连接(remote/clone)
今天是Git系列课程第二课,上一课我们已经学会在本地创建一个空仓库,痞子衡今天要讲的是如何将本地仓库与远程建立联系. 1.将本地仓库挂上远程git remote 本地建好了仓库,我们希望能够挂到远程服 ...
- Mongodb副本集--Out of memory: Kill process 37325 (mongod)
1.Mongodb副本集--Out of memory: Kill process 37325 (mongod) 场景描述: 恢复一个22TB数据的mongodb实例的时候. 将备用结点加入mongo ...
- SQL Server分页存储过程通用存储过程
CREATE proc [dbo].[p_paging]@tableName varchar(8000), --表名.视图名@indexCol varchar(50) = 'id', ...
- 杭电ACM2005--第几天?
第几天? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 从零开始学安全(三十六)●利用python 爆破form表单
import sys import requests from requests.auth import HTTPBasicAuth def Brute_Force_Web(postData): re ...
- php 爬虫框架
发现两款不错的爬虫框架,极力推荐下: phpspider 一款优秀的PHP开发蜘蛛爬虫 官方下载地址:https://github.com/owner888/phpspider 官方开发手册:http ...