使用代碼為textview設置drawableLeft


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) 
 
 


免責聲明!

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



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