簡體   English   中英

android獲取已設置為textview的drawabletop的drawable

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM