gpt4 book ai didi

java - 使用数据绑定(bind)更改背景图像

转载 作者:行者123 更新时间:2023-11-30 00:38:15 26 4
gpt4 key购买 nike

在 gradle 中我启用了数据绑定(bind)。尝试更改背景图片后,当我运行应用程序时,我在日志中观察到以下错误:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Identifiers must have user defined types from the XML file. ImageBacground is missing it
file:D:\Bogv\Android project\Pote\app\src\main\res\layout\fragment1.xml
loc:13:19 - 13:32
****\ data binding error ****

我的 View 类:

    public class ImageBacground {
private ImageView imageView;

public ImageBacground(ImageView imageView) {
this.imageView = imageView;

}
@BindingAdapter({"android:src"})
public static void loadImage(ImageView view, String imageUrl) {
Picasso.with(view.getContext())
.load(imageUrl)
.placeholder(R.drawable.potehki_fon)
.into(view);
}

public ImageView getImageView() {
return imageView;
}

public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
}

XML

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable name="images" type="com.retrofa.potehkilulibalse.images.ImageBacground"/>
</data>
<LinearLayout
android:id="@+id/first_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:gravity="center"
android:orientation="horizontal"
android:src="@{ImageBacground.loadImage,default=@drawable/potehki_fon}"
android:theme="@style/AppTheme.NoActionBar"

>

<ImageView

android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_weight="1"
android:clickable="true"
android:src="@drawable/eda" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_weight="1"
android:clickable="true"
android:src="@drawable/umivanie" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_weight="1"
android:clickable="true"
android:src="@drawable/son" />
</LinearLayout>
</layout>

最佳答案

您需要创建自定义绑定(bind)适配器

 public class CustomBinding {

@BindingAdapter({"imageUrl"})
public static void loadImage(LinearLayout layout, Drawable drawable) {
if (drawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(drawable);
} else {
layout.setBackgroundDrawable(drawable);
}
}
}
}

然后在xml中你可以这样使用

activity_main.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:imageUrl="@{@drawable/ic_android_black_24dp}">
</LinearLayout>
</layout>

现在,在 Activity 文件的onCreate()方法中绑定(bind)xml布局

DataBindingUtil.setContentView(this, R.layout.activity_main);

关于java - 使用数据绑定(bind)更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com