使用代码为textview设置drawableLeft

简介: xml中的textView中设置android:drawableLeft:   如程序中所见我在xml中设置了 DrawableLeft。我想在代码中改变drawable。有什么方法可以使用代码为textview设置drawableLeft呢? 解决方案 public void  setC...

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) 
 
 
目录
相关文章
|
24天前
|
Java Android开发
TextView设置跑马灯效果
TextView设置跑马灯效果
19 0
|
XML 编解码 数据格式
如何正确设置动态TextView的textSize
如何正确设置动态TextView的textSize
158 0
如何正确设置动态TextView的textSize
|
XML Android开发 数据格式
Android ProgressBar进度条,以及自定义显示
Android ProgressBar进度条,以及自定义显示
360 0
Android ProgressBar进度条,以及自定义显示
|
XML Java Android开发
Android中TextView与EditText控件实现禁止换行
Android中TextView与EditText控件实现禁止换行
1975 0
|
Android开发
Android--TextView 文字显示和修改
  一. 新建一个Activity 和 Layout   首先在layout文件夹中新建一个activity_main.xml,在新建工程的时候一般默认会新建此xml文件,修改其代码如下: activity_main.
1409 0
|
Android开发 数据格式 XML
Android 代码中动态设置字体大小-TextView.SetTextSize()
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/51898155 ...
2933 0