在代码中修改TextView的DrawableRight图片

TextView的xmlandroid

  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设置的图片能够使用.net

setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)xml

Drawable能够经过 Drawable nav_up=getResources().getDrawable(R.drawable.button_nav_up);获得对象

可是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法,因而咱们加一行代码就能够了blog

  1. nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  


代码合在一块儿是这样的:图片

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