首先,我们在开发过程中,会经常使用到android:drawableLeft="@drawable/ic_launcher"这些类似的属性:

关于这些属性的意思,无非是在你的textView文本的上下左右处添加一个图片。比如下面这么一段代码:

 

<TextView

android:id="@+id/text_drawable"
android:layout_width="wrap_content"

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);

Drawable drawableLeft = getResources().getDrawable(

R.drawable.ic_launcher);

textDrawable.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,

null, null, null);

textDrawable.setCompoundDrawablePadding(4);

效果和以上直接通过android:drawableLeft一样!

Android 动态设置TextView的drawableLeft等属性的更多相关文章

  1. android 动态设置TextView值,例:金额添加

    一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...

  2. android 动态设置TextView值,例:金额增加

    一说到动态递增设置TextView值,很多人应该马上就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值但是效率不咋 ...

  3. Android中设置TextView的颜色setTextColor

    tv.setTextColor(Color.parseColor("#FFFFFF")); tv.setTextColor(Color.WHITE); tv.setTextColo ...

  4. 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  5. 【转】Android中设置TextView的颜色setTextColor

    原文网址:http://www.cnblogs.com/myphoebe/archive/2012/01/06/2314728.html android中设置TextView的颜色有方法setText ...

  6. [转]Android中设置TextView的颜色setTextColor

    [转自]http://txlong-onz.iteye.com/blog/1249609 Android中设置TextView的颜色setTextColor android中设置TextView的颜色 ...

  7. Android如何设置TextView的行间距、行高。

         Android系统中TextView默认行间距比较窄,不美观.     我们可以设置每行的行间距,可以通过属性android:lineSpacingExtra或android:lineSpa ...

  8. C#动态设置匿名类型对象的属性

    用C#写WPF程序, 实现功能的过程中碰到一个需求: 动态设置对象的属性,属性名称是未知的,在运行时才能确定. 本来这种需求可以用 Dictionary<string, object> 实 ...

  9. Android 动态设置控件获取焦点

    之前写过一篇博客,简单的介绍了Android 隐藏EditText的焦点,之所以要隐藏EditText的焦点,是因为当应用在第一次进入某个Activity时,由于该页面中的EditText获取了焦点, ...

随机推荐

  1. ThreadPoolExecutor系列二——ThreadPoolExecutor 代码流程图

    ThreadPoolExecutor 代码流程图 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7681648.ht ...

  2. leetcode — remove-duplicates-from-sorted-array

    import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/remove-duplicates-from-sort ...

  3. Python迭代和解析(2):迭代初探

    解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html 在Python中支持两种循环格式:while和for.这两种循环的类型不 ...

  4. 使用codis-admin搭建codis集群

    目的 在Redis Codis 部署安装的文章中,介绍了通过fe在web上搭建codis的基本步骤和方法,也介绍了codis-admin的相关说明,为了更好的熟悉codis-admin的使用,本文将使 ...

  5. keil常用配置设置

    1.设置tab键为空格4个进入Configuration->Editor,设置如下: 2.代码自动补齐进入Configuration->Editor,设置如下: 3.使用快捷键实现批量注释 ...

  6. Java原型模式

    原型模式 原型模式也称克隆模式.原型模式jian ming zhi yi,就是先创造出一个原型,然后通过类似于Java中的clone方法,对对象的拷贝,克隆类似于new,但是不同于new.new创造出 ...

  7. SpringBoot究竟是如何跑起来的?

    摘要: 神奇的SpringBoot. 原文:SpringBoot 究竟是如何跑起来的? 作者:老钱 Fundebug经授权转载,版权归原作者所有. 不得不说 SpringBoot 太复杂了,我本来只想 ...

  8. arcgis for js学习之Graphic类

    arcgis for js学习之Graphic类 <title>Graphic类</title> <meta charset="utf-8" /> ...

  9. 自定义HorizontalScrollView的scrollBar

    尊重劳动成果,转载请标明出处http://www.cnblogs.com/tangZH/p/8423803.html android滑动组件的scrollBar,看了不是很顺眼,没办法,因为项目需求, ...

  10. JHipster技术栈定制 - JHipster Registry配置信息加密

    本文说明了如何开启和使用JHipster-Registry的加解密功能. 1 整体规划 1.1 名词说明 名词 说明 备注 对称加密 最快速.最简单的一种加密方式,加密(encryption)与解密( ...