如何使用DataBinding将Image资源设置为ImageView

Geo*_*mas 53 xml android android-databinding

我们如何在android中使用数据绑定将图像资源放入ImageView

  <ImageView
            android:id="@+id/is_synced"
            android:src="@{model.pending ? @mipmap/pending: @mipmap/synced}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

如果pending为true,我想要一个图像,如果pending为false,我想要另一个图像.但它显示错误.我如何实现此功能?

qin*_*iao 48

回答:

限定:

@BindingAdapter({"android:src"})
public static void setImageViewResource(ImageView imageView, int resource) {
    imageView.setImageResource(resource);
}
Run Code Online (Sandbox Code Playgroud)

使用:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:scaleType="center"
    android:src="@{viewModel.imageRes, default=@drawable/guide_1}"/>
Run Code Online (Sandbox Code Playgroud)


lut*_*oid 39

像这样设置图像,

  <ImageView
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:src="@{model.isActive ? @drawable/white_activated_icon :@drawable/activated_icon}"
        tools:src="@mipmap/white_activated_icon" />
Run Code Online (Sandbox Code Playgroud)


Spa*_*Bao 37

我试过这个,它对我有用(buildToolsVersion:24.0.1):

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_margin="8dp"
    android:scaleType="centerInside"
    app:imageResource="@{item.avatarResId}"/>
Run Code Online (Sandbox Code Playgroud)

只是app:imageResource用来替换android:src,android:src="@{item.avatarResId}"不起作用,@BindAdapter("android:src")为它定义一个自定义.

但是使用app:imageResource不需要@BindAdapter另外定义,因为ImageView有一个名为的方法setImageResource(),当你使用时app:imageResource,它会setImageResource()自动调用.


yoA*_*ex5 7

如果您想将参数传递给方法,就像@IdRes一样,您可以使用

<?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>

        <import type="<package_name>.R" />
    </data>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        app:load_image="@{R.drawable.image}" />
</layout>
Run Code Online (Sandbox Code Playgroud)


Roh*_*eja 6

如果数据模型包含图像的资源ID,则无需以编程方式执行任何操作即可执行此操作.

例:

<layout>

<data>
<import type="android.support.v4.content.ContextCompat" />            
<variable
name="roomInfoItem"
type="com.example......model.RoomInfoModel" /> </data>

<RelativeLayout> 

 <ImageView
    android:id="@+id/iv_room_info_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 />

</RelativeLayout>

</layout>
Run Code Online (Sandbox Code Playgroud)

只需将以下行添加到imageView(我在那里添加代码时无法格式化代码):

android:src="@{ContextCompat.getDrawable(context,roomInfoItem.resId)}"
Run Code Online (Sandbox Code Playgroud)

哪里resId包含R.drawable.your_image_name


pio*_*543 5

对于这篇伟大的文章:使用数据绑定和毕加索加载图像

有两种方法可以做到:

  1. 运用 @BindingAdapter
  2. ObservableField 和定制的毕加索目标

在Android开发人员参考数据绑定指南中,您只能找到第一个.

请阅读这两篇文章.

更多信息:

希望它有所帮助.