如果我们在每个xml文件中都把相同的布局都重写一遍,一个是代码冗余,可读性很差;另一个是修改起来比较麻烦,对后期的修改和维护非常不利。所以,一般情况下,我们需要把相同布局的代码单独写成一个模块,然后在用到的时候,可以通过<include /> 标签来重用layout的代码。

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

图 标题栏的示例

 

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

实现代码如下:

<RelativeLayout

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:background="@drawable/navigator_bar_bg"

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

    <TextView

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

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:gravity="center"

        android:hint="title"

        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView

        android:id="@android:id/closeButton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:src="@drawable/close" />

</RelativeLayout>

以上就是【解析Android开发优化之:对界面UI的优化详解(二)】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论