在代碼中修改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);  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM