本文为菜鸟学习笔记兼翻译练习用,翻译可能会不准确,细节请以原文为准,如有不足之处还请见谅,若能斧正,小弟不胜感激。原文地址: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.xml和 contact.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>
下一篇是一个比较长的小结,主要是数据绑定支持的运算符和语法的介绍。
本文介绍如何在Android中利用数据绑定技术将变量传递给引入的布局文件,并解释了数据绑定库支持的数据传递方式及限制。
在引用布局中使用数据绑定&spm=1001.2101.3001.5002&articleId=78655059&d=1&t=3&u=fdb0dc8913a1401d840c50eabba97533)
950

被折叠的 条评论
为什么被折叠?



