在代码中修改TextView的DrawableRight图片

TextView的xml
[html] view plain copy
 
  1. <TextView  
  2.                 android:id="@+id/textciew1"  
  3.                 android:layout_width="match_parent"  
  4.                 android:layout_height="wrap_content"  
  5.                 android:background="#000"  
  6.                 android:drawableRight="@drawable/button_nav_down"  
  7.                 android:gravity="center_vertical"  
  8.                 android:paddingLeft="16dp"  
  9.                 android:paddingRight="16dp"  
  10.                 android:text="展开"  
  11.                 android:textColor="#fff"  
  12.                  />  
在代码中如果要修改drawableRight设置的图片可以使用

 

setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)
Drawable可以通过 Drawable nav_up=getResources().getDrawable(R.drawable.button_nav_up);得到

 

但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法,于是我们加一行代码就可以了

 

[java] view plain copy
 
  1. nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  

代码合在一起是这样的:

 

 

[java] view plain copy
 
  1. Drawable nav_up=getResources().getDrawable(R.drawable.button_nav_up);  
  2. nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  
  3. textview1.setCompoundDrawables(null, null, nav_up, null);  
posted @ 2016-02-17 14:29  新感觉  阅读(5888)  评论(0编辑  收藏  举报