当前位置:操作系统 > 安卓/Android >>

[Android] Android开发优化之——对界面UI的优化(2)

在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件。一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局。如果我们在每个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  www.zzzyk.com
        android:id="@android:id/closeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/close" />
</RelativeLayout>
 
    我们将上面的xml文件命名为“navigator_bar.xml”,其它需要标题栏的Activity的xml布局文件就可以直接引用此文件了。
<include layout="@layout/navigator_bar" />
 
经验分享:
一般情况下,在项目的初期就能够大致确定整体UI的风格。所以早期的时候就可以做一些规划,将通用的模块先写出来。
下面是可能可以抽出的共用的布局:
1)背景。有的应用在不同的界面里会用到统一的背景。后期可能会经常修改默认背景,所以可以将背景做成一个通用模块。
2)头部的标题栏。如果应用有统一的头部标题栏,就可以抽取出来。
3)底部的导航栏。如果应用有导航栏,而且大部分的Activity的底部导航栏是相同的,就可以将导航栏写成一个通用模块。
4)ListView。大部分应用都会用到ListView展示多条数据。项目后期可能会经常调整ListView的风格,所以将ListView作为一个通用的模块比较好。
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,