如何在Android中使用<layout>标签

Lum*_*oid 8 xml layout android android-databinding

我需要知道如何在android xml文件中使用layout标签.我知道它用于数据绑定,但我对此没有完整的了解.如果有人可以帮助我,请告诉我.

提前致谢 !!

Lui*_*rra 15

使用时,<layout>标签必须是根标签DataBinding.这样做就是告诉编译器你正在使用DataBinding,你的布局会有像<variable><import>那样的特殊标签,所以你必须在你的标签中嵌入你的布局.

简而言之,<layout>只要您使用DataBinding编译器来理解特殊标记并DataBinding使用正确的变量和方法生成类,就需要使用标记.

如果你有这样的布局(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>
Run Code Online (Sandbox Code Playgroud)

它基于<layout>标记内部的内容来创建具有变量及其getter和setter 的LayoutDataBinding类(自动生成)User.