程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-關於在TextView中設置DrawableLeft的問題

android-關於在TextView中設置DrawableLeft的問題

編輯:編程綜合問答
關於在TextView中設置DrawableLeft的問題

xml中的textView:

<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.代碼中動態變化
Drawable drawable= getResources().getDrawable(R.drawable.drawable);
/// 這一步必須要做,否則不會顯示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
myTextview.setCompoundDrawables(drawable,null,null,null);

也或參考另一個函數

public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top, Drawable right, Drawable bottom)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved