如何使用< layout>在Android中标记 [英] How to use <layout> tag in Android

查看:83
本文介绍了如何使用< layout>在Android中标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在android xml文件中使用布局标签.我知道它用于数据绑定,但是我对此并不完全了解.请让我知道是否有人可以帮助我.

I need to know how to use layout tag in android xml file. I know it is used for data binding but I do not have complete knowledge on this. Please let me know if anyone can help me in same.

预先感谢!

推荐答案

使用DataBinding时,<layout>标记必须是根标记.这样做是告诉编译器您正在使用DataBinding,并且布局将具有特殊的标签,例如<variable><import>,因此您必须将布局嵌入到该标签中.

The <layout> tag must be the root tag when you are using DataBinding. Doing so you are telling the compiler that you are using DataBinding and your layout will have special tags like <variable> or <import>, so you have to embed your layout within that tag.

简而言之,每当使用DataBinding进行编译器时,都需要使用<layout>标记,以了解特殊标记并使用正确的变量和方法生成DataBinding类.

In short, you need to use the <layout> tag whenever you are using DataBinding for the compiler to understand the special tags and generate the DataBinding class with the right variables and methods.

如果您具有这样的布局( layout_data_binding.xml ):

If you have a layout like this (layout_data_binding.xml):

<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

它基于<layout>标记内的内容,使用User变量及其获取器和设置器创建LayoutDataBinding类(自动生成的).

It is based on what is inside the <layout> tag to create the LayoutDataBinding class (auto-generated) with the User variable and its getters and setters.

这篇关于如何使用&lt; layout&gt;在Android中标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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