Android数据绑定不适用于包含的布局

问题描述

我想重用文本字段并将它们作为独立的组件分别保存在XML文件中。我正在使用数据绑定来绑定字符串资源。

如果未将这些组件拆分为独立的XML文件,则效果很好,但是现在,因为我使用了include标签,所以在App中不显示任何资源字符串。有什么问题吗?

代码示例:

主要布局

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="Customres" type="com.project.utils.Customresources"/>
    </data>

   <LinearLayout
       android:id="@+id/mainContentLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:padding="@dimen/padding_medium">

           <include layout="@layout/form_name"/>

           <include layout="@layout/form_email"/>

           <include layout="@layout/form_phone"/>

   </LinearLayout>

</layout>

包含的布局示例:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="Customres" type="com.project.utils.Customresources"/>
    </data>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/nameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextInputLayoutStyle"
        app:errorEnabled="true"
        android:hint='@{Customres.stringValues["form_name"]}'>

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameEt"
            android:layout_width="match_parent"
            android:inputType="textCapWords"
            android:singleLine="true"
            app:errorEnabled="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            style="@style/TextInputEditTextStyle"/>

    </com.google.android.material.textfield.TextInputLayout>

</layout>

解决方法

您需要将ID添加到include标记中:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
   android:id="@+id/mainContentLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="@dimen/padding_medium">

       <include layout="@layout/form_name"
                android:id="@+id/name_layout_include"/>
       <include layout="@layout/form_email"/>
       <include layout="@layout/form_phone"/>

  </LinearLayout>

包含的布局示例:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextInputLayoutStyle"
        app:errorEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/name_et"
            android:layout_width="match_parent"
            android:inputType="textCapWords"
            android:singleLine="true"
            app:errorEnabled="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            style="@style/TextInputEditTextStyle"/>

    </com.google.android.material.textfield.TextInputLayout>
</layout>

然后您可以通过以下方式解决该问题:

mBinding.nameLayoutInclude.nameEt

由于Viewbinding比Databinding更适合未来使用,因此最好采用这种方式。

,

尝试一下

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="CustomRes" type="com.project.utils.CustomResources"/>
    </data>

   <LinearLayout
       android:id="@+id/mainContentLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:padding="@dimen/padding_medium">

           <include 
           app:CustomResIncluded ="@{CustomRes}"
           layout="@layout/form_name"/>

   </LinearLayout>

</layout>

include_layout

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="CustomResIncluded" type="com.project.utils.CustomResources"/>
    </data>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/nameLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextInputLayoutStyle"
        app:errorEnabled="true"
        android:hint='@{CustomResIncluded.stringValues["form_name"]}'>

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/nameEt"
            android:layout_width="match_parent"
            android:inputType="textCapWords"
            android:singleLine="true"
            app:errorEnabled="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            style="@style/TextInputEditTextStyle"/>

    </com.google.android.material.textfield.TextInputLayout>

</layout>