通过数据绑定将字符串传递给包含的布局不起作用 [英] Passing string to included layout via data binding not working

查看:55
本文介绍了通过数据绑定将字符串传递给包含的布局不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Android数据绑定功能将简单的字符串从主布局传递到布局.它可以正常编译,但是传递给include的值实际上并未传递.即-它不会显示在我的布局中.

I am trying to pass a simple string to an layout from my main layout using the Android data binding feature. It compiles fine, but the value being passed to the include is not actually being passed. i.e. - it does not show up in my layout.

<?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">

    <data>
        <variable name="mytitle" type="java.lang.String"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:background="@color/black_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

 <include
            android:id="@+id/include1"
            layout="@layout/mylayout"
            app:mytitle="@{@string/categories}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    </android.support.constraint.ConstraintLayout>
</layout>

我的包含布局是:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="mytitle" type="java.lang.String"/>
    </data>

<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/black_background"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/category_header_textview"
        style="@style/WhiteText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{mytitle}"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
</layout>

推荐答案

确定.弄清楚了.在这里为其他人发布. 我所缺少的实际上是使用DataBindingUtil设置内容视图.

OK. Figured it out. Posting here for others. The thing I was missing was actually using the DataBindingUtil to set the content view.

将以下内容添加到您的onCreate()中:

Add the following to your onCreate():

DataBindingUtil.setContentView(this, R.layout.mylayout);

这篇关于通过数据绑定将字符串传递给包含的布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆