DataBinding setDrawable 无法显示的问题

  • 问题:dataBinding中drawableLeft无效

  • 解决办法:采用传统代码设置

  • 思路:

1、使用代码设置Drawable有两种方式。
drawable.setBounds(int,int,int,int);//未设置则不显示
view.setCompoundDrawables(drawable,null,null,null);

view.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);

2、查看生成的binding代码发现采用setCompoundDrawables方式,且未设置Bounds

  • 使用到的代码

    android:drawableRight=”@{1==employee.state ?@drawable/ic_job : @drawable/ic_job_quit}”

TextViewBindingAdapter

@BindingAdapter({"android:drawableRight"})
public static void setDrawableRight(TextView view, Drawable drawable) {
    Drawable[] drawables = view.getCompoundDrawables();
    view.setCompoundDrawables(drawables[0], drawables[1], drawable, drawables[3]);
}