android - 通过数据绑定将字符串传递给包含的布局不起作用
问题描述
我正在尝试使用 Android 数据绑定功能将一个简单的字符串从我的主布局传递给一个布局。它编译得很好,但是传递给包含的值实际上并没有被传递。即 - 它没有出现在我的布局中。
<?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 来设置内容视图。
将以下内容添加到您的 onCreate() 中:
DataBindingUtil.setContentView(this, R.layout.mylayout);
推荐阅读
- android - WebSetting allowFileAccessFromFileURLs 在 API 级别 30 中已弃用
- visual-studio - 安装 Microsoft.PowerShell.SDK 后,我的项目中添加了一个“运行时”文件夹
- bash - 是否可以在 trap 命令中使用 Bash 历史扩展?
- python - Django - 对包含 recaptcha 的表单进行单元测试
- azure - 在 Azure 容器实例 (ACI) 中运行 Gitlab Runner
- java - Android工作室,回收视图
- python - 与 tensorflow 2 的深度互相关
- python - 用于使用 LSTM 进行情感分析的多类分类的 Keras 模型 - 如何改进我的模型?
- javascript - 将单选按钮值添加到总和变量(javascript)
- javascript - 安装 Facebook SDK for Android 后如何获取数据