使用代码为textview设置drawableLeft

简介:

xml中的textView中设置android:drawableLeft:

<TextView         

android:id="@+id/bookTitle"        

android:layout_width="match_parent"        

android:layout_height="wrap_content"        

android:layout_weight="1"       

android:drawableLeft="@drawable/checkmark"         

android:gravity="center_vertical"        

android:textStyle="bold"         

android:textSize="24dip"       

android:maxLines="1"          

android:ellipsize="end"/>  


如程序中所见我在xml中设置了 DrawableLeft。
我想在代码中改变drawable。
有什么方法可以使用代码为textview设置drawableLeft呢?

解决方案

public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);  


类似调用方法如下:

1.在XML中使用

android:drawableLeft="@drawable/icon"  


2.代码中动态变化


  1. Drawable drawable= getResources().getDrawable(R.drawable.drawable);  
  2. /// 这一步必须要做,否则不会显示.  
  3. drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
  4. myTextview.setCompoundDrawables(drawable,null,null,null);  


也或参考另一个函数

  public  void setCompoundDrawablesWithIntrinsicBounds (Drawable left,  Drawable top, Drawable right, Drawable bottom) 

本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/p/3735590.html如需转载请自行联系原作者

欢醉
相关文章
|
17天前
|
Java Android开发
TextView设置跑马灯效果
TextView设置跑马灯效果
19 0
|
11月前
|
Android开发
Android控件 TextView属性大全
Android控件 TextView属性大全
|
XML 编解码 数据格式
如何正确设置动态TextView的textSize
如何正确设置动态TextView的textSize
158 0
如何正确设置动态TextView的textSize
|
Android开发
Android 动态添加View 并设置id
Android 动态添加View 并设置id
519 0
Android 动态添加View 并设置id
|
Android开发
Android Switch控件修改样式
Android Switch控件修改样式
Android Switch控件修改样式
|
Android开发
Android--TextView 文字显示和修改
  一. 新建一个Activity 和 Layout   首先在layout文件夹中新建一个activity_main.xml,在新建工程的时候一般默认会新建此xml文件,修改其代码如下: activity_main.
1409 0