【问题标题】:android get the drawable that has sett as drawabletop of textviewandroid获取已设置为textview的drawabletop的drawable
【发布时间】:2018-08-22 15:43:23
【问题描述】:

我有文本视图

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:drawableTop="@drawable/new" />

我想在活动 jave 上知道 drawableTop 上设置了哪个图像,如何?

【问题讨论】:

    标签: android textview


    【解决方案1】:

    使用

     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 时比较它们的好方法,对吧?
    • 可能是对的,但我在这个线程 stackoverflow.com/questions/9125229/… 上找到了这个答案
    • getCompoundDrawables() 以指定的顺序返回它们 Left、Top、Right、Bottom
    【解决方案2】:

    首先,为您的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];
    

    【讨论】:

      【解决方案3】:

      我认为你应该使用 textView.getCompoundDrawables();至于从可绘制顶部获取,我认为这将是第二个可绘制对象,例如

      Drawables tmp[] = textView.getCompoundDrawables();
      tmp[1];  //this should be your top drawable but not 100% sure.
      

      【讨论】:

      • 你改了问题)
      【解决方案4】:

      您无法在运行时获取可绘制 ID。 TextView 仅在其构造函数中使用 ID 来加载相应的可绘制对象。加载drawable后ID消失了。

      在某些情况下根本没有可绘制的 ID。 drawableTop 可以是颜色 rgb 值。

      【讨论】:

      • 一个RGB值变成ColorDrawable
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多