在代码中修改TextView的DrawableRight图片

来源:互联网 发布:2017优化设计英语答案 编辑:程序博客网 时间:2024/04/30 20:55


TextView的xml

[html] view plaincopy
  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 plaincopy
  1. nav_up.setBounds(00, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  

代码合在一起是这样的:

[java] view plaincopy
  1. Drawable nav_up=getResources().getDrawable(R.drawable.button_nav_up);  
  2. nav_up.setBounds(00, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());  
  3. textview1.setCompoundDrawables(nullnull, nav_up, null);  
0 0
原创粉丝点击