gpt4 book ai didi

android-databinding - 安卓数据绑定(bind): how to pass variable to include layout

转载 作者:行者123 更新时间:2023-12-03 14:07:28 26 4
gpt4 key购买 nike

谷歌文档说变量可能会从包含布局传递到包含布局的绑定(bind)中,但我无法使其工作,但会出现数据绑定(bind)错误****msg:标识符必须具有来自 XML 文件的用户定义类型。处理程序缺少它。
包含的 XML 如下所示:

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

<data>
<import type="com.example.FocusChangeHandler"/>

<variable
name="handler"
type="FocusChangeHandler"/>
</data>

<!-- Some other views --->

<include
android:id="@+id/inputs"
layout="@layout/input_fields"
bind:handler="@{handler}"/>
</layout>

并且包含的​​ XML 是这样的:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/nameEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onFocusChange="@{handler.onFocusChange}"/>
</layout>

我可以通过生成的绑定(bind)类从包含的布局中引用 View ,但传递变量不起作用。

最佳答案

Just create <variable for passing values to included layouts.

Like app:passedText="@{@string/app_name}"


例子
就像我想通过 String包括布局。我将创建一个 String 类型的变量.引用 String给您的 TextView .我创建了 passedText例如。
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
>

<data>
// declare fields
<variable
name="passedText"
type="String"/>
</data>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{passedText}"/> //set field to your view.

</layout>
现在添加 passedText字段到您的 <include标签。
<?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
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<include
layout="@layout/layout_common"
app:passedText="@{@string/app_name}" // here we pass any String
/>

</LinearLayout>
</layout>
请注意,两种布局(父版和包含版)都应为 binding layout , 用 <layout 包裹

关于android-databinding - 安卓数据绑定(bind): how to pass variable to include layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com