繁体   English   中英

Android中维度资源的绑定适配器

[英]Binding adapter for dimension resource in Android

我有一个 ImageView,我想为其编写一个自定义绑定适配器

    <ImageView
        android:id="@+id/item_list_picture"
        android:layout_height="@dimen/normal_475"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_above="@id/item_list_secondary_text"
        android:contentDescription="@{listItemViewModel.primaryText}"
        android:padding="@dimen/normal_100"
        android:displayImageUrl="@{listItemViewModel.pictureUrl}"
        android:placeHolderDrawable="@{@drawable/ic_baseline_image_24}"
        android:layout_width="@dimen/normal_475"
        android:specificWidth="@dimen/normal_475"
        android:goneUnless="@{listItemViewModel.pictureUrl != null}" />

specificWidth的尺寸在dimens.xml中定义为

<dimen name="normal_475">77dp</dimen>

最后, BindingAdapter.kt文件包含以下代码:

@BindingAdapter("android:displayImageUrl", "android:placeHolderDrawable", "android:specificWidth")
fun loadImage(view: ImageView, displayImageUrl: String?, placeHolderDrawable: Drawable, @DimenRes width: Int) {
    if (!displayImageUrl.isNullOrEmpty()) {
        Picasso.get().load(displayImageUrl).placeholder(placeHolderDrawable)
            .resize(view.context.resources.getDimensionPixelSize(width), view.context.resources.getDimensionPixelSize(width)).centerCrop().into(view)
    } else {
        view.setImageDrawable(placeHolderDrawable)
    }
}

如果我使用没有specificWidth的绑定适配器的loadImage ,一切正常。 否则,我会收到以下错误

ERROR:C:\dev\projects\myapp\app\src\main\res\layout\item_list.xml:69: AAPT: error: attribute android:specificWidth not found.

这里有什么问题? 如何从定义的尺寸中获取特定宽度? 我还尝试将android:layout_width添加到绑定适配器而不是我的自定义android:specificWidth ,但它也不起作用。 在 dp 中使用android:layout_width值的可能性会更好,因为我需要获得ImageView的大小以调整从pictureUrl下载的图像的大小。

因为android:specificWidth的类型是资源,你需要像下面这样设置它:

android:specificWidth="@{R.dimen.normal_475}"

您还需要在数据标签中导入R

<import type="your.package.name.R" />

或者,如果您想传递 dimen 值,您需要将bindingAdapter specificWidth输入更改为此:

fun loadImage(
view: ImageView,
displayImageUrl: String?,
placeHolderDrawable: Drawable,
width: Float // change the type to float and clear the `@dimenRes` annotation

还将您的 xml 更改为:

 android:specificWidth="@{@dimen/normal_475}" // you need to pass the value in data-binding format

顺便说一下,您也可以像您所说的那样忽略这个android:specificWidth属性,而是从视图的宽度和高度中读取值。 您只需要在view.post{}上执行此操作以确保呈现视图并且其大小有效。 所以你的bindingAdapter乐趣将是这样的:

@BindingAdapter("android:displayImageUrl", "android:placeHolderDrawable")
fun loadImage(view: ImageView, displayImageUrl: String?, placeHolderDrawable: Drawable) {
    view.post {
        if (!displayImageUrl.isNullOrEmpty()) {
            Picasso.get().load(displayImageUrl).placeholder(placeHolderDrawable)
                .resize(
                    view.width,
                    view.height
                ).centerCrop().into(view)
        } else {
            view.setImageDrawable(placeHolderDrawable)
        }
    }
}

如果您有任何问题,或者我没有很好地解决您的问题,请随时提出。

对我有用的解决方案是将特定宽度定义为android:specificWidth="@{@dimen/normal_475}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM