【问题标题】:Data binding: How pass Context in xml to method?数据绑定:如何将 xml 中的上下文传递给方法?
【发布时间】:2018-05-12 21:59:09
【问题描述】:

Android Studio 3.0。

这是我的自定义方法:

public static int getTileWidthDpInScreen(Context context) {
    // some code here
    return tileWidthDp;
}

这里是我的带有数据绑定代码的 xml 文件:

<?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="com.myproject.android.customer.util.GUIUtil" />

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="@{GUIUtil.getTileWidthDpInScreen()}"
            android:layout_height="@dimen/preview_image_height"
            android:scaleType="centerCrop"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />       


    </android.support.constraint.ConstraintLayout>

</layout>

结果我得到错误:

e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
data binding error msg:cannot find method getTileWidthDpInScreen() in class com.myproject.android.customer.util.GUIUtil

这个错误是因为我没有在方法 getTileWidthDpInScreen() 中传递上下文。

我如何在 xml 中获取上下文并在方法中传递它:getTileWidthDpInScreen()?

【问题讨论】:

  • 好的。这是工作:A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name。因此,这里的工作代码:android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}"
  • 很高兴帮助你@alex

标签: android android-databinding


【解决方案1】:

Android documentation 说:

生成一个名为context 的特殊变量,用于绑定 根据需要表达。上下文的值是 Context 对象 从根视图的getContext() 方法。 context 变量是 被具有该名称的显式变量声明覆盖。

所以解决方案是: android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}

【讨论】:

    【解决方案2】:

    您只需在 xml 数据导入中导入 &lt;import type="android.content.Context" /&gt;

    然后,您只需使用 context 即可访问数据绑定的上下文。

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
            <import type="android.content.Context" />
    
            <import type="com.myproject.android.customer.util.GUIUtil" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/imageViewPhoto"
                android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}"
                android:layout_height="@dimen/preview_image_height"
                android:scaleType="centerCrop"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    

    【讨论】:

    • 不需要导入Context,数据绑定已经把变量context添加到作用域了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多