【问题标题】:Pass data to <include> layout using Databinding使用数据绑定将数据传递给 <include> 布局
【发布时间】:2020-05-17 00:31:15
【问题描述】:

我今天一直在搜索这个,但只找到了一些关于这个问题的不清楚的答案。

我在一个表单中有多个 TextView 和 EditText,所有这些都是由我的主题自定义的,我想每次都重用相同的包含布局,但要像 TextView 中的文本或提示一样为其设置参数在 EditText 内。

我现在的 form.xml:

<LinearLayout android: orientation="horizontal">
     <TextView android:text="Username"/>
     <EditText android:id="edittext_username"
               android:hint="Enter username..."
               android:inputType="text"/>
</LinearLayout>

<LinearLayout android: orientation="horizontal">
     <TextView android:text="Password"/>
     <EditText android:id="edittext_password"
               android:hint="Enter password..."
               android:inputType="password"/>
</LinearLayout>

... other fields ...

我想要的主要 form.xml 中的内容:

<LinearLayout android:orientation="vertical" ...>
     <include layout="form_edittext" 
            app:textview_text="@{`Username`}"
            app:edittext_hint="@{`Enter username...`}"
            ... other parameters ...
 />
     <include layout="form_edittext"
            app:textview_text="@{`Password`}"
            app:edittext_hint="@{`Enter password...`}"
            ... other parameters ...
 />
</LinearLayout>

还有form_edittext.xml:

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

    <data>
        <variable name="textview_text" type="java.lang.String"/>
        <variable name="edittext_id" type="java.lang.String"/>
        <variable name="edittext_inputType" type="java.lang.String"/>
        <variable name="edittext_hint" type="java.lang.String"/>
    </data>

    <TextView android:text="@{textview_text}"/>
    <EditText android:id="@{edittext_id}"
                   android:hint="@{edittext_hint}"
                   android:inputType="@{edittext_inputType}"/>
</layout>

我还是一个初学者,我不知道这是否可能。 在这篇文章中,这个人回答说他使用了数据绑定(如我所示的示例) How to Re-using Layouts with <include/> with parameters?(你可能会看到第一个答案)。

但是,使用这种方法会给我一个错误,即 XML 无法识别诸如“textview_text”之类的标识符(我通过 @{...} 访问的变量。如果您有其他解决方案,如果您分享它们,我将不胜感激. 干杯!

更新 iCantC 的答案:(但 textview 的文本和提示仍然为空)。 主要布局:

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

 ...
            <include layout="@layout/activity_login_layout_input"
                android:id="@+id/activity_login_layout_input_emailAddress"
                app:textviewt="@{@string/email}"     //the string is "Email Address"
                app:edittexth="@{@string/emailhint}" />     //the string is "Your email address..."

 ...
</layout>

包含的布局:

<layout>

    <data>
        <variable
            name="textviewt"
            type="String"/>
        <variable
            name="edittexth"
            type="String"/>
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:showDividers="middle">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{textviewt}"
            android:textColor="@color/c1"
            android:textSize="12sp"/>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingEnd="26dp">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/edittext"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center|start"
                android:layout_marginStart="26dp"
                android:background="@drawable/activity_login_background"
                android:hint="@{edittexth}"
                android:padding="6dp"
                android:textAlignment="center"
                android:textColor="@color/c2"
                android:textColorHint="@color/grey"
                android:textSize="14sp"/>

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="start"
                android:adjustViewBounds="true"
                android:src="@drawable/activity_login_edittext_drawable" />

        </FrameLayout>

    </LinearLayout>

</layout>

【问题讨论】:

    标签: android xml data-binding parameters include


    【解决方案1】:

    您似乎想包含一个通用布局,然后将动态参数从主布局传递到包含的布局,这是步骤,

    第 1 步:在您的项目中启用 DataBinding

    //In the build.gradle file in the app module, add this 
    
    android {
        ...
        dataBinding {
            enabled = true
        }
    }
    
    

    第 2 步:创建您的 common_layout_included.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    
    <data>
        <variable  
            name="username"
            type="String"/>
    </data>
    
    <LinearLayout
            ..
            >
    
    <TextView
        android:id="@+id/tv_username"
        android:text="@{username}"/> 
    
       </LinearLayout>
    
    </layout>
    

    第 3 步:在您的 main_layout.xml 中包含 common_layout_included.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <LinearLayout
            ..
            >
    
            <include
                android:id="@+id/includedLayout"
                layout="@layout/common_layout_included"
                app:username="@{@string/username}" // here we pass any String 
                />
    
        </LinearLayout>
    </layout>
    
    

    第 4 步:确保使用 DataBinding 方式扩展布局

    //In the onCreate of your Activity
    
    val binding = DataBindingUtil.setContentView(this,R.layout.main_layout)
    
    

    就是这样,你可以走了。如果仍然出现一些错误,请执行 File -&gt; Invalidate Caches/Restart

    最后一件事,我看到您正在将 id 动态分配给视图的 android:id="@{edittext_id}",根据我的所有经验,我从未真正遇到过有动力这样做的用例。我什至不知道这是否可能,即使可能,我也怀疑这是一个好习惯。

    【讨论】:

    • 嗨,我一直在关注每一步。我没有收到任何编译错误,但即使我在 include 中传递了用户名字符串“johnny”,“johnny”也不会在运行时出现在包含布局的 textview 文本中,文本仍然为空。为什么会这样?
    • 你能添加两个更新的布局吗?我去看看。
    • 请看主要问题。我已经把我更新的代码放在最后还不能工作。我正在尝试将 textview 的文本设置为“电子邮件地址”,并将 edittext 的提示设置为“您的电子邮件地址 ...”,这两个字符串都在我的 string.xml 中。但是,这两个字段在运行时都保持为空。
    • 让我们继续在这个房间里聊天chat.stackoverflow.com/rooms/214066/databinding-issue
    • 嘿,如果有帮助,请接受答案。未来的读者可能会从中受益。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多