【发布时间】:2020-04-02 15:47:32
【问题描述】:
我正在尝试将<include> 标记与数据绑定一起使用以重用一种常见布局。
每个重复使用的布局都必须设置不同的标签。
为此,我尝试将此值作为数据绑定参数 (bind:test) 传递。
当我尝试在包含的 xml (sync_row.xml) 中使用从父 xml (sync_fragment.xml) 接收的变量时,会出现问题。
在"android:text='@{test}" 的行中,Android Studio xml 语法在 'test' 下划线并说:“找不到标识符 'test'”。
与我所做的文档唯一不同的是,不要从 Android Studio 中的 Android SDK 菜单中检查 Android 支持(因为我在列表中没有此条目!)。
我该如何解决这个问题?
谢谢。
Android Studio 版本:3.6.1
Gradle 版本:5.6.4
文件:sync_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="12">
<include
android:id="@+id/includedLayout1"
layout="@layout/sync_row"
bind:test='@{"TEST_DATA_BIND1"}' />
<include
android:id="@+id/includedLayout2"
layout="@layout/sync_row"
bind:test='@{"TEST_DATA_BIND2"}' />
<include
android:id="@+id/includedLayout3"
layout="@layout/sync_row"
bind:test='@{"TEST_DATA_BIND3"}' />
</LinearLayout>
</layout>
文件:sync_row.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="test" type="String" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{test}'/>
</LinearLayout>
</layout>
Gradle 文件:
...
dataBinding {
enabled = true
}
...
编辑:
我刚刚将 Android Studio 版本更新到 3.6.2。
红色高亮问题似乎消失了。
我仍然无法在片段上打印值,始终为空字符串!
我还尝试在sync_row.xml 中强制输出,直接设置bind:text='@{"hello"}'。没有成功,视图中仍然打印空标签。
Gradle 版本:5.6.4
【问题讨论】:
标签: java android android-studio android-layout data-binding