我在support Lib v23.2中使用了新的vector drawable support,其应用程序为:srcCompat&;试图通过数据绑定设置其可绘制性
<;布局xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto“>;
<;数据>;
<;变量
name=“mediaPojo”
type=“in.ishaan.pika.data\u binding.MediaPojo”/>;
</数据>;
<;相对布局
android:background=“@color/black”
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”>;
<;播放视频
... /&燃气轮机;
<;图片框
...
app:srcCompat=“@{mediaPojo.isPlaying?@drawable/ic\u pause\u 24dp:@drawable/ic\u play\u arrow\u 24dp}”
/&燃气轮机;
<;进度条
…/>;
</相对寿命>;
</布局>;
在尝试构建时,studio抛出:
错误:(33,30)找不到参数类型为android.graphics.drawable.drawable的属性“app:srcCompat”的setter
当您通过数据绑定设置向量资源时,只需使用android:src属性而不是compat属性
数据绑定库生成在运行时执行setImageResource方法的类
<;图片框
...
android:src=”https://stackoverflow.com/questions/35766898/@{@drawable/your_drawable}”
/&燃气轮机;
据http://android-developers.blogspot.com/2016/02/android-support-library-232.html setImageResource方法可以在运行时在较旧的系统版本上使用,而无需进行任何其他更改
如果您想使用app:srcCompat属性。您必须定义@BindingMethods注释,该注释将属性与ImageView中的适当setter连接起来。例如,在活动或片段中添加
@BindingMethods({
@BindingMethod(type=android.widget.ImageView.class,
attribute=“app:srcCompat”,
方法=“setImageDrawable”)})
公共类MainActivity扩展了AppCompatActivity{
//你的身体在这里
}