在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件。一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局。如果我们在每个xml文件中都把相同的布局都重写一遍,一个是代码冗余,可读性很差;另一个是修改起来比较麻烦,对后期的修改和维护非常不利。所以,一般情况下,我们需要把相同布局的代码单独写成一个模块,然后在用到的时候,可以通过<include /> 标签来重用layout的代码。

常见的,有的应用在最上方会有一个标题栏。类似下图所示。

图 标题栏的示例

如果项目中大部分Activity的布局都包含这样的标题栏,就可以把标题栏的布局单独写成一个xml文件。

  1. <RelativeLayout

  2.    android:layout_width="fill_parent"

  3.    android:layout_height="wrap_content"

  4.    android:gravity="center"

  5.    android:background="@drawable/navigator_bar_bg"

  6.    xmlns:android="http://schemas.android.com/apk/res/android">

  7.    <TextView

  8.        android:id="@android:id/title"

  9.        android:layout_width="fill_parent"

  10.        android:layout_height="wrap_content"

  11. android开发,青软培训