使用代码为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如需转载请自行联系原作者

欢醉
相关文章
|
Java Android开发 数据格式
使用代码为TextView设置drawableLeft
直接上代码:   类似调用方法如下:1.在XML中使用 Java code   android:drawableLeft="@drawable/icon" 2.
684 0
|
Android开发 数据格式 XML
Android 代码中动态设置字体大小-TextView.SetTextSize()
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/51898155 ...
3188 0
|
Android开发
一个自定义的android TextView控件例子
如题,文件链接:/Files/cappuccino/Todo_List.rar
598 0
|
Android开发 数据安全/隐私保护
Android自定义View示例(一)—带有删除按钮的EditText
MainActivity如下: package cc.textview5; import android.os.Bundle; import android.
1036 0
|
XML 编解码 数据格式
如何正确设置动态TextView的textSize
如何正确设置动态TextView的textSize
217 0
如何正确设置动态TextView的textSize

热门文章

最新文章