我有一个自定义布局,我想在多个地方重复使用,所以我希望能够为其传递标题,因为这是唯一会实际更改的值。我知道我可以通过绑定数据来做到这一点,但是我无法获取要渲染的数据。
在我的activity.main
中,我有:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.r.e.MainActivity">
<include
layout="@layout/switch_preference_custom_title"
app:passedTitle="@{@string/hello_world}" />
</RelativeLayout>
</layout>
我定义了如下自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<data>
<variable
name="passedTitle"
type="String"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{passedTitle}" />
</RelativeLayout>
</layout>
我还添加了
dataBinding {
enabled = true
}
在我的build.gradle中。我的项目可以编译并正常运行,并且布局确实可以渲染(我可以通过为其指定背景),但是文本是一个空字符串。
答案 0 :(得分:0)
似乎缺少最后一个链接。而不是
QT_ARCH
在MainActivity中,需要声明
setContentView(R.layout.activity_main);
这会将数据绑定库与根布局连接起来,没有它,您的任何数据绑定都将无法实际呈现。