[英]android get the drawable that has sett as drawabletop of textview
我有textview
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="@drawable/new" />
我想在活動jave上知道哪個圖像在drawableTop
上設置了,怎么樣?
采用
Drawable[] drawables = textView.getCompoundDrawables();
另外如果你想比較兩個drawable。
Bitmap bitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
首先,為TextView
一個ID,以便在Activity
找到它:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="@drawable/new" />
其次,在Activity
找到你的TextView
並在調用setContentView
后獲取onCreate
方法中的復合drawable:
TextView textView = (TextView) findViewById(R.id.textView);
// Left, top, right, bottom drawables.
Drawable[] compoundDrawables = textView.getCompoundDrawables();
// This is the top drawable.
Drawable topCompoundDrawable = compoundDrawables[1];
我認為你應該使用textView.getCompoundDrawables(); 至於從可繪制頂部獲取我會認為它將是第二個可繪制的例如
Drawables tmp[] = textView.getCompoundDrawables();
tmp[1]; //this should be your top drawable but not 100% sure.
你無法在運行時獲得可繪制的ID。 TextView僅在其構造函數中使用ID來加載相應的drawable。 在繪制后加載ID已消失。
在某些情況下,根本沒有可繪制的ID。 drawableTop可以是顏色rgb值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.