Android布局技巧
Android平台提供了大量的UI构件,你可以将这些小的视觉块(构件)搭建在一起,呈现给用户复杂且有用的画面。然而,应用程序有时需要一些高级的视觉组件。为了满足这一需求,并且能高效的实现,你可以把多个标准的构件结合起来成为一个单独的、可重用的组件。
例如,你可以创建一个可重用的组件包含一个进度条和一个取消按钮,一个Panel包含两个按钮(确定和取消动作),一个Panel包含图标、标题和描述等等。简单的,你可以通过书写一个自定义的View来创建一个UI组件,但更简单的方式是仅使用XML来实现。
在Android XML布局文件里,一般,每个标签都对应一个真实的类实例(这些类一般都是View的子类)。UI工具包还允许你使用三个特殊的标签,它们不对应具体的View实例:、、。这篇文章将描述如何使用来创建一个单纯的XML视觉组件。了解更多关于如何使用的资料,请参看《合并布局》文章,尤其是它与结合起来使用体现出来的强大威力。
元素的作用如同它的名字一样;它用于包含其它的XML布局。使用这个标签如下面的例子所示:
<com.android.launcher.Workspace
android:id="@+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:defaultScreen="1">
<include  android:id ="@+id/cell1"
layout ="@layout/workspace_screen" />
<include  android:id ="@+id/cell2"
layout ="@layout/workspace_screen" />
<include  android:id ="@+id/cell3"
layout ="@layout/workspace_screen" />
</com.android.launcher.Workspace> 在中,只需要layout 特性。这个特性,不带android 命名空间前缀,它表示你想包含的布局的引用。在这个例子中,相同的布局被包含了三次。这个标签还允许你重写被包含布局的一些特性。上面的例子显示了你可以使用android:id 来指定被包含布局中根View 的id ;它
还可以覆盖已经定义的布局id 。相似的,你可以重写所有的布局参数。这意味着任何android:layout_*的特性都可以在中使用。下面是例子: 1
2
3
<include  android:layout_width ="fill_parent"
layout ="@layout/image_holder" />  <include  android:layout_width ="256dip" layout ="@layout/image_holder" /> 这个标签,在依据设备设置定制UI 时表现得尤为有用。举个例子,Activity 的主要布局放置在layout /文件夹下,其它布局放置在layout -land/和layout -port/下。这样,在垂直和水平方向时你可以共享大多数的UI 布局。
Android UI 工具包提供了一些布局管理器,它们使用起来相当容易,而且,大多数的时候,你只需要使用它们最基本的特征来实现UI 。
执着于基本特征的使用对于创建UI 来说,往往不是最高效的。一个常见的例子就是滥用LinearLayout ,它将会导致View 树中的View 数量激增。View ——更糟的是,布局管理器——添加到应用程序里都会带来一定的消耗:初始化,布局和绘制变得更加缓慢。嵌套布局的花销尤其“昂贵”,例如,如果你嵌套了
一些LinearLayout ,并使用了weight 参数,这会导致子元素要计算两次。
让我们看一个非常简单且常见的布局例子:一个列表项,左边是一个图标,右边是标题和描述,上方是标题,下方是可选的描述。列表项可能看起来如下图:
为了清楚地认识View之间(一个ImageView和两个TextView)的相对位置,下图是使用HierarchyViewer 抓获的布局剪影:
实现这个布局,直接使用LinearLayout就可以了。列表项本身是一个水平的LinearLayout,里面有一个ImageView和一个垂直的LinearLayout,垂直的LinearLayout里包含两个TextView。以下是这个布局的源代码:
<LinearLayout
xmlns:android="schemas.android/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHei ght"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"/>
</LinearLayout>
</LinearLayout>
如果你将它作为ListView的item,它能正常工作,但却是相当浪费的。相同的布局可以使用RelativeLayout 进行重写,相对于每个列表项来说,可以节省一个View,且View层级上更好,只有一层。使用RelativeLayout 也很简单:
<RelativeLayout
xmlns:android="schemas.android/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHei ght"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/icon"/>
android layout布局<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="@id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@id/secondLine"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="My Application"/>
</RelativeLayout>
新的实现与老的实现看起来效果完全一致,除了一种情况。每个列表项显示两行文字:标题和可选的描述。当某一个列表项的描述不可获得时,应用程序可能希望将第二个TextView的Visibility设为GONE。LinearLayout实现版表现得很完美,但RelativeLayout实现版就有点差强人意了:
在RelativeLayout里,每个View都是和父元素RelativeLayout对齐或是和其它View对齐的。例如,我们声明描述部分是和RelativeLayout的底部对齐,标题位于其上并与RelativeLayout的顶端对齐。当描述GONE时,RelativeLayout不知道怎么去放置标题的底边缘。为了解决这个问题,你可以使用一个非常简单的布局参数:layout_alignWithParentIfMissing。
这个布尔参数告诉RelativeLayout:如果目标对象消失时使用自己的边缘作为锚点。例如,如果你放置一个View到另一个Visibiity属性设为GONE的View的右边,且设定alignWithParentIfMissing为true,RelativeLayout就会将其左边缘作为View的对齐锚点。在我们的这个场合,使用alignWithParentIfMissin
g的结果是RelativeLayout将标题部分的底部与自己的底部对齐。结果如下所示:
现在,我们的布局表现得很完美了,即使描述部分的Visibility属性设为GONE。更好的是,层级更加简单,因为我们不再使用LinearLayout,而且,更加高效了。当我们使用HierarchyViewer来比较两个实现版的时候,事实就更明显了: