虽然 Android 提供不少小的可重用的交互组件,你仍然可能须要重用复杂一点的组件,这也许会用到 Layout。为了高效重用整个的 Layout,你可使用 <include/> 和 <merge/> 标签把其余 Layout 嵌入当前 Layout。html
重用 Layout 很是强大,它让你能够建立复杂的可重用 Layout。好比,一个 yes/no 按钮面板,或者带有文字的自定义进度条。这也意味着,任何在多个 Layout 中重复出现的元素能够被提取出来,被单独管理,再添加到 Layout 中。因此,虽然能够添加一个自定义 View 来实现单独的 UI 组件,你能够更简单的直接重用某个 Layout 文件。android
建立可重用 Layoutapp
若是你已经知道你须要重用的 Layout,就先建立一个新的 XML 文件并定义 Layout 。好比,如下是一个来自 G-Kenya codelab 的 Layout,定义了一个须要添加到每一个 Activity 中的标题栏(titlebar.xml):性能
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=”match_parent” android:layout_height="wrap_content" android:background="@color/titlebar_bg"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo" /> </FrameLayout>
根节点 View 就是你想添加入的 Layout 类型。code
使用<include>标签xml
使用 <include> 标签,能够在 Layout 中添加可重用的组件。好比,这里有一个来自 G-Kenya codelab 的 Layout 须要包含上面的那个标题栏:htm
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=”match_parent” android:layout_height=”match_parent” android:background="@color/app_bg" android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> <TextView android:layout_width=”match_parent” android:layout_height="wrap_content" android:text="@string/hello" android:padding="10dp" /> ... </LinearLayout>
你也能够覆写被添加的 Layout 的全部 Layout 参数(任何 android:layout_* 属性),经过在 <include/> 中声明他们来完成。好比:blog
<include android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/title"/>
然而,若是你要在 <include> 中覆写某些属性,你必须先覆写 android:layout_height 和 android:layout_width。get
使用<merge>标签string
<merge /> 标签在你嵌套 Layout 时取消了 UI 层级中冗余的 ViewGroup 。好比,若是你有一个 Layout 是一个竖直方向的 LinearLayout,其中包含两个连续的 View 能够在别的 Layout 中重用,那么你会作一个 LinearLayout 来包含这两个 View ,以便重用。不过,当使用一个 LinearLayout 做为另外一个 LinearLayout 的根节点时,这种嵌套 LinearLayout 的方式除了减慢你的 UI 性能外没有任何意义。
为了不这种状况,你能够用 <merge> 元素来替代可重用 Layout 的根节点。例如:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/add"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </merge>
如今,当你要将这个 Layout 包含到另外一个 Layout 中时(而且使用了 <include/> 标签),系统会忽略 <merge> 标签,直接把两个 Button 放到 Layout 中 <include> 的所在位置。