将数据从一种布局传递到另一种布局

Passing data from one layout to another

我正在尝试通过传递数字(即 1-9)和数字下方的一些字母(即 "abc"、"def" 等)来重用键盘按钮。出于某种原因,我无法获得那个工作。您可以在下面看到代码和我所做的一切。我已经在互联网上阅读了所有内容,但就是无法正常工作。

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="mainText"
            type="String"/>
        <variable
            name="subtitleText"
            type="String"/>
    </data>
    <FrameLayout android:layout_height="85dp"
        android:layout_width="85dp"
        android:background="@drawable/button_bg_round">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@{mainText}"
            android:textSize="44sp"
            android:fontFamily="@font/latoregular"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingRight="2dp"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@{subtitleText}"
            android:textSize="15sp"
            android:fontFamily="@font/latoregular"
            android:gravity="center"
            android:paddingTop="50dp"
            android:paddingRight="2dp"
            />

    </FrameLayout>
</layout>

我在这里重复使用该组件

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto">
...

 <include
            android:id="@+id/button_1"
            layout="@layout/keypad_layout"
            app:mainText="@{@string/keyboard_1}"
            bind:mainText ="@{@string/keyboard_1}"
            app:subtitleText="@{@string/keyboard_1_subtitle}"
            />
...
</layout>

最后,我在 build.gradle 中添加了启用的数据绑定。

我在这里找到了答案 - Include layout with custom attributes

为了使其工作,您需要记住必须启用数据绑定并且必须通过 DataBindingUtil.inflate 对其进行扩充,而不仅仅是常规视图扩充。但是,是的,它有效。