【问题标题】:Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView在 android.widget.ImageView 上找不到参数类型为 int 的属性“android:layout_width”的设置器
【发布时间】:2017-12-01 08:45:18
【问题描述】:

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 android-databinding


    【解决方案1】:

    不幸的是,默认情况下,此语法不支持 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}"            
    

    【讨论】:

    • 更短的甚至可以使它成为 ViewGroup 上的扩展功能,如下所示:@BindingAdapter("layoutWidth") fun ViewGroup.setLayoutWidth(dimen: Float) { layoutParams.Width = dimen.toInt() }
    【解决方案2】:

    在你的 XML 中应该是这样的

    app:layout_width="@{GuiUtil.getTileWidthDpInScreen(context), default=wrap_content}"
    

    【讨论】:

    • 没有帮助。错误:Cannot find the setter for attribute 'android:layout_width' with parameter type float on android.widget.ImageView.
    • 您可以使用相同的旧行进行绑定注释@BindingAdapter("layout_width")
    • 帮不上忙:error: attribute 'com.myproject.android.customer.debug:layout_width' not found.
    【解决方案3】:

    尝试将它用于您的 BindingAdapter:

    @BindingAdapter("android:layout_width")
    

    【讨论】:

    • 无济于事:我收到错误::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 float on android.widget.ImageView.
    • 您是否更改了 BindingAdapter 以接受浮点数而不是 int?
    • 没有。我没变
    • 尝试改为接受浮点数而不是整数。
    • 是的,我设置了浮点数和这个帮助:@BindingAdapter("android:layout_width") public static void setLayoutWidth(View view, float width) { ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.width = (int) width; view.setLayoutParams(layoutParams);}
    【解决方案4】:

    在布局中添加:app:layoutWidth 所需的值对我有用: 喜欢:

    android:layout_width="wrap_content"   
    android:layout_height="wrap_content"   
    app:layoutWidth="@{testBoolean == true ? @dimen/value_1 : @dimen/value_2}"   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 2017-03-04
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多