[英]****/ data binding error ****msg:Cannot find the setter for attribute 'app:image_url' with parameter type java.lang.String on android.widget.ImageView
[英]Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView
在app/build.gradle
:
dataBinding {
enabled = true
}
kapt "com.android.databinding:compiler:3.0.1"
在布局中,我有兩個圖像。
我想設置寬度僅為第一張圖像。
這是 XML 布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.myproject.android.customer.util.GuiUtil" />
</data>
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="@{GuiUtil.getTileWidthDpInScreen(context), default=@dimen/preview_image_height}"
android:layout_height="@dimen/preview_image_height"/>
<ImageView
android:id="@+id/imageViewFavorite"
android:layout_width="28dp"
android:layout_height="28dp"/>
</layout>
這是適配器的代碼:
@BindingAdapter("layout_width")
public static void setLayoutWidth(View view, int width) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = width;
view.setLayoutParams(layoutParams);
}
這是GuiUtil.getTileWidthDpInScreen
方法:
public class GuiUtil {
public static int getTileWidthDpInScreen(Context context) {
// some logic that return int value
}
但我收到此錯誤:
:app:transformDataBindingWithDataBindingMergeArtifactsForDebug UP-TO-DATE
:app:kaptDebugKotlin
e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView.
file:myproject\app\src\main\res\layout\preview_offer_item.xml
loc:26:36 - 26:74
****\ data binding error ****
at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
不幸的是,默認情況下,Android 數據綁定不支持此語法。 您可以改用@BindingAdapter,以下是完整代碼(Kotlin):
@BindingAdapter("layoutWidth")
fun setLayoutWidth(layout: ViewGroup, dimen: Float) {
val layoutParams = layout.layoutParams
layoutParams.width = dimen.toInt()
layout.layoutParams = layoutParams
}
在您的 XML 布局文件中使用“app:layoutWidth”,您甚至可以使用其他語法來使用它,例如:
android:layout_width="match_parent" //required
android:layout_height="@dimen/dp_102" //required
app:layoutWidth="@{variable.predict ? @dimen/dp_86 : @dimen/dp_102}"
在你的 XML 中它應該是這樣的
app:layout_width="@{GuiUtil.getTileWidthDpInScreen(context), default=wrap_content}"
嘗試將其用於您的 BindingAdapter:
@BindingAdapter("android:layout_width")
添加: app:layoutWidth 在布局中具有所需的值對我有用:例如:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutWidth="@{testBoolean == true ? @dimen/value_1 : @dimen/value_2}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.