android - Android - 在 XML 复合视图组件中创建新视图属性并传递给其子级 - 布局中的数据绑定
问题描述
完整答案:
1. 在app/build.gradle中启用数据绑定:
dataBinding {
enabled true
}
2.使用DataBindingUtil设置内容视图
爪哇
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBindingUtil.setContentView(this, R.layout.activity_engineering_mode_main);
}
3.子项布局
你会看到我定义了 2 个新属性
值/bools.xml
<variable
name="textTitle"
type="String" />
<variable
name="buttonVisibility"
type="boolean" />
使用textTitle,您可以通过@string/string_name使用资源中的任何字符串
使用buttonVisibility,您必须定义bool资源类型,并使用@bool/bool_name
布局/item_engineering_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="textTitle"
type="String" />
<variable
name="buttonVisibility"
type="boolean" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/engineer_actionbar_height"
android:background="@color/engineer_background_color"
android:orientation="horizontal">
<TextView
android:id="@+id/engineer_txtName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/engineer_text_margin"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@{textTitle}"
android:textColor="@color/engineer_text_color"
android:textSize="@dimen/engineer_title_font_size" />
<Button
android:id="@+id/engineer_btnNext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/engineer_text_margin"
android:text="BACK"
android:visibility="@{buttonVisibility ? View.VISIBLE : View.GONE, default=gone}" />
</LinearLayout>
</layout>
4.布尔资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="item_button_visibility_default">false</bool>
<bool name="item_button_visibility_on">true</bool>
<bool name="item_button_visibility_off">false</bool>
</resources>
5. 父级布局,包括一些子级并将值传递给新属性
<?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"
android:orientation="vertical">
<include
android:id="@+id/includedLayout0"
layout="@layout/item_engineering_list_row"
app:buttonVisibility="@{@bool/item_button_visibility_on}"
app:textTitle="@{@string/app_name}" />
<include
android:id="@+id/includedLayout1"
layout="@layout/item_engineering_list_row"
app:buttonVisibility="@{@bool/item_button_visibility_default}"
app:textTitle="@{@string/app_name}" />
</LinearLayout>
</layout>
原始问题:
我是 android 新手,我在 QT 中使用 QML 已经有一段时间了。
我想知道如何通过在复合视图组件中应用 XML 中的参数来使布局更容易。
我在 xml 中有一个自定义布局项,并且希望将一些属性从父级传递给其子级,并且我还想用新值初始化父级的属性以自定义其子级。
我的概念如下:
项目.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
!!!! how to declare a new attribute here !!!
| like this:
| textTitle="New Title" // default value for child
| buttonVisibility="visible" // default value for child
">
<TextView
android:id="@+id/engineer_txtTitle"
android:text= textTitle <--- use parent's />
<Button
android:id="@+id/engineer_btnBack"
android:visibility= buttonVisibility <== use parent's />
</LinearLayout>
点击这里查看图片:基础项目
主要的.xml
<LinearLayout>
<include
android:id="@+id/item1"
layout="@layout/item"
textTitle= "FIRST"
// buttonVisibility not set here, use default as visible
/>
<include
android:id="@+id/item2"
layout="@layout/item"
textTitle= "SECOND"
buttonVisibility = "gone" // dont show button
/>
</LinearLayout>
单击此处查看图片:使用参数申请
解决方案
您可以使用架构组件的数据绑定。这是您的要求的示例。
最近我回答了一个与此相关的问题。 干净的答案
此示例显示将值传递给<include
.
我有一个共同的观点layout_common.xml
,我想传递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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{passedText}"/> //set field to your view.
</layout>
现在您可以将passedText
字段传递给您的<include
标签。
activity_main.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
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/includedLayout"
layout="@layout/layout_common"
app:passedText="@{@string/app_name}" // here we pass any String
/>
</LinearLayout>
</layout>
请注意,两个布局(父和包含)都应该是binding layout
,即用<layout>
和</layout>
标签包裹
推荐阅读
- ios - 从firebase调用数据需要完成处理程序?
- sql - 未使用 string_to_array(long_string, ',') 上的 Postgres 功能索引
- python - 无法在 SQL Alchemy 中返回 Postgres 表名
- excel-formula - 从列中的每个单元格中查找日期值,并根据日期在两个日期之间的位置返回年份日期
- c# - 在 TableLayoutPanel 的同一行内的控件内设置控件
- c# - 从位图对象制作更小的位图
- javascript - this.setState 不覆盖旧状态?
- node.js - SyntaxError:编译 ejs 时出现意外的标记
- flutter - 该方法未为该类定义 - Flutter
- sql - 如何使用标准 SQL 将日期时间转换为 Google Bigquery 中的日期,或者如何从日期时间的 where 语句中提取年份?