[英]Android Data binding is not working for included layout
我想重用文本字段並將它們作為獨立組件單獨保存在 XML 文件中。 我正在使用數據綁定來綁定字符串資源。
如果這些組件沒有拆分成獨立的 XML 文件,它工作得很好,但是現在我使用了 include 標簽,它不會在 App.js 中顯示任何資源字符串。 有什么問題?
代碼示例:
主要布局
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.