繁体   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