简体   繁体   English

Android - 在 XML 复合视图组件中创建新的视图属性并传递给其子级 - 布局中的数据绑定

[英]Android - Make new view attribute and pass to its children in XML compound view component - data binding in layout

Full answer:完整答案:

1. Enable data binding in app/build.gradle : 1. 在app/build.gradle 中启用数据绑定:

dataBinding {
   enabled true
}

2. Use DataBindingUtil to set content view 2.使用DataBindingUtil设置内容视图

java爪哇

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DataBindingUtil.setContentView(this, R.layout.activity_engineering_mode_main);
}

3. Child item layout 3. 子项布局

You will see that I define 2 new attributes你会看到我定义了 2 个新属性

values/bools.xml值/ bools.xml

<variable
    name="textTitle"
    type="String" />

<variable
    name="buttonVisibility"
    type="boolean" />

With textTitle , you can use any string from resource by @string/string_name使用textTitle ,您可以通过@string/string_name使用资源中的任何字符串

With buttonVisibility , you have to define bool resource type, and use @bool/bool_name使用buttonVisibility ,您必须定义bool资源类型,并使用@bool/bool_name

layout/item_engineering_list_row.xml布局/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. Boolean resource file 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. Parent layout, which includes some children and passes value to new attributes 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>

Original Question:原问题:

I am new to android and I've been working with QML in QT for a time.我是 android 新手,我一直在 QT 中使用 QML。

I wonder how I can make a layout more easier by applying params in XML in compound view components.我想知道如何通过在复合视图组件中应用 XML 中的参数来使布局更容易。

I have a custom layout item in xml and want to pass some attributes from a parent to its children, and I also want to initialize parent's attribute with new values to customize its children too.我在 xml 中有一个自定义布局项,并希望将一些属性从父级传递给其子级,并且我还想用新值初始化父级的属性以自定义其子级。

My concept is as below:我的概念如下:

item.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>

CLICK HERE TO SEE IMAGE: base Item单击此处查看图像:基本项目

main.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>

CLICK HERE TO SEE IMAGE: apply with param单击此处查看图像:使用参数申请

You can use Data Binding of Architecture component.您可以使用架构组件的数据绑定 Here is an sample of your requirement.这是您的要求的示例。

Recently I answered a question related to this.最近我回答了一个与此相关的问题。 Clean answer干净的答案

This example shows pass value to <include .此示例显示将值传递给<include

I have a common view layout_common.xml , I want to pass String to included layout.我有一个通用视图layout_common.xml ,我想将String传递给包含的布局。 I will create a variable of type String .我将创建一个String类型的变量。 Refer that String to your TextView .将该String引用到您的TextView I created passedText for example.例如,我创建了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>

Now you can pass passedText field to your <include tag.现在您可以将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>

Note that both layouts (parent & included) should be binding layout , ie wrapped with <layout> and </layout> tags请注意,两个布局(父级和包含)都应该是binding layout ,即用<layout></layout>标签包裹

Thanks @Khemraj to show the keyword "Data Binding" in Android :)感谢@Khemraj在 Android 中显示关键字“数据绑定”:)

I have found the answer for me.我已经为我找到了答案。 It includes Khemraj's answer and some small code added to values resource.它包括 Khemraj 的答案和一些添加到 values 资源的小代码。 I posted it in my question for others to find it easily.我将它发布在我的问题中,以便其他人轻松找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM