无法通过数据绑定(@BindingAdapter)加载图像:AAPT:错误:属性

a_subscriber

Android Studio 3.6

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    viewBinding {
        enabled = true
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

在某些类(例如MyAdapter)中,我创建loadImage带有注释的公共静态方法@BindingAdapter

public class MyAdapter extends DataBindingRecyclerViewAdapter {

    public MyAdapter(Context context, List<?> data) {
        super(context, data);
    }

    @Override
    protected int getLayoutForPosition(int position) {
        return R.layout.forecast_data_list_item;
    }

    @BindingAdapter({"app:weatherImage"})
    public static void loadImage(ImageView view, String imageUrl) {
        Glide.with(view.getContext())
                .load(imageUrl).apply(new RequestOptions().circleCrop())
                .into(view);
    }

}

在xml中:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="model"
            type="com.myproject.model.SomeModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/invoiceMainContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:layout_margin="@dimen/half_default_margin"
            android:scaleType="centerCrop"
            android:src="@drawable/default_image"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/containerRight"
            app:layout_constraintStart_toStartOf="parent"
            app:weatherImage="http://www.gravatar.com/avatar/11111?s=200x200"
            app:layout_constraintTop_toTopOf="parent" />

但是我得到了编译错误:

AAPT: error: attribute weatherImage (aka com.myproject:weatherImage) not found.

在您的BindingAdapter中

 @BindingAdapter("app:weatherImage")
    @JvmStatic
    fun setSelectedBackGround(view: ImageView, imageUrl: String) {
        Glide.with(view.getContext())
            .load(imageUrl).apply(new RequestOptions().circleCrop())
            .into(view);
    }

并在您的XMl中像这样使用它

<ImageView
            android:id="@+id/imageView"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:layout_margin="@dimen/half_default_margin"
            android:scaleType="centerCrop"
            android:src="@drawable/default_image"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/containerRight"
            app:layout_constraintStart_toStartOf="parent"
            app:weatherImage="@{`http://www.gravatar.com/avatar/11111?s=200x200`}"
            app:layout_constraintTop_toTopOf="parent" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android 数据绑定 BindingAdapter 错误

来自分类Dev

通过数据属性选择通过Ajax加载的元素

来自分类Dev

通过数据属性选择通过Ajax加载的元素

来自分类Dev

通过数据绑定API设置众所周知的属性值

来自分类Dev

通过数据绑定API设置众所周知的属性值

来自分类Dev

Vuejs 通过数据属性动态加载指令

来自分类Dev

通过数据绑定监听事件

来自分类Dev

有没有办法在视图创建期间通过数据绑定读取视图属性?

来自分类Dev

通过数据属性查找元素

来自分类Dev

通过数据属性选择(JQuery)

来自分类Dev

无法通过数据属性数据背景关闭引导程序模式

来自分类Dev

通过数据绑定从UI更改数据

来自分类Dev

Android通过数据绑定库动态包含布局

来自分类Dev

通过数据绑定到ItemsControl自动生成内容

来自分类Dev

通过数据加载器枚举时发生KeyError

来自分类Dev

缩放通过数据库导入的图像

来自分类Dev

通过数据数组链接背景图像

来自分类Dev

通过数据属性循环元素并替换值

来自分类Dev

通过数据属性查找并填写输入字段

来自分类Dev

如何使用jQuery通过数据属性过滤选项?

来自分类Dev

通过数据属性查找和删除项目

来自分类Dev

通过数据属性设置每个DIV的高度

来自分类Dev

通过数据属性查找和删除项目

来自分类Dev

如何通过数据属性过滤选择框?

来自分类Dev

通过数据属性设置每个DIV的高度

来自分类Dev

通过数据属性循环元素并替换值

来自分类Dev

JQuery - 通过数据属性 = 变量查找元素

来自分类Dev

通过数据排序

来自分类Dev

通过数据。navigationController