Google Data Binding Library 谷歌官方数据绑定库(三)在引用布局中使用数据绑定

来源:互联网 发布:医药行业ims数据分析 编辑:程序博客网 时间:2024/06/14 01:10

本文为菜鸟学习笔记兼翻译练习用,翻译可能会不准确,细节请以原文为准,如有不足之处还请见谅,若能斧正,小弟不胜感激。原文地址:Google Data Binding Library

引用布局

变量可以在一个属性中通过使用应用的命名空间和变量名传递给一个引入的布局:

<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:bind="http://schemas.android.com/apk/res-auto">   <data>       <variable name="user" type="com.example.User"/>   </data>   <LinearLayout       android:orientation="vertical"       android:layout_width="match_parent"       android:layout_height="match_parent">       <include layout="@layout/name"           bind:user="@{user}"/>       <include layout="@layout/contact"           bind:user="@{user}"/>   </LinearLayout></layout>

现在, name.xmlcontact.xml布局文件中都应有一个user变量。

数据绑定不支持通过merge元素直接引入作为子控件。例如,下边的布局中的使用方法是不支持的:

<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:bind="http://schemas.android.com/apk/res-auto">   <data>       <variable name="user" type="com.example.User"/>   </data>   <merge>       <include layout="@layout/name"           bind:user="@{user}"/>       <include layout="@layout/contact"           bind:user="@{user}"/>   </merge></layout>

下一篇是一个比较长的小结,主要是数据绑定支持的运算符和语法的介绍。

阅读全文
0 0
原创粉丝点击