android获取已设置为textview的drawabletop的drawable

Wil*_*aan 23 android textview

我有textview

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:drawableTop="@drawable/new" />
Run Code Online (Sandbox Code Playgroud)

我希望活动jave知道哪个图像已经设置drawableTop,如何?

Nim*_*ary 26

使用

 Drawable[] drawables = textView.getCompoundDrawables();
Run Code Online (Sandbox Code Playgroud)

另外如果你想比较两个drawable.

Bitmap bitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();

if(bitmap == bitmap2)
    {
        //Code blcok
    }
Run Code Online (Sandbox Code Playgroud)

  • getCompoundDrawables()以指定的顺序返回它们Left,Top,Right,Bottom (9认同)
  • 我不认为这是一个很好的方法让应用程序创建两个位图并比较TextView上的每次点击,对吧? (2认同)

Jos*_*arl 10

首先,提供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" />
Run Code Online (Sandbox Code Playgroud)

其次,在您Activity找到TextViewonCreate在调用后获取方法中的复合drawable setContentView:

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];
Run Code Online (Sandbox Code Playgroud)