Android布局技巧之创建高效布局

Android布局技巧之创建高效布局,第1张

概述AndroidUI工具包提供了一些布局管理器,它们使用起来相当容易,而且,大多数的时候,你只需要使用它们最基本的特征来实现UI。

AndroID UI工具包提供了一些布局管理器,它们使用起来相当容易,而且,大多数的时候,你只需要使用它们最基本的特征来实现UI。

执着于基本特征的使用对于创建UI来说,往往不是最高效的。一个常见的例子就是滥用linearLayout,它将会导致VIEw树中的VIEw数量激增。VIEw――更糟的是,布局管理器――添加到应用程序里都会带来一定的消耗:初始化,布局和绘制变得更加缓慢。嵌套布局的花销尤其“昂贵”,例如,如果你嵌套了一些linearLayout,并使用了weight参数,这会导致子元素要计算两次。

让我们看一个非常简单且常见的布局例子:一个列表项,左边是一个图标,右边是标题和描述,上方是标题,下方是可选的描述。列表项可能看起来如下图:

为了清楚地认识VIEw之间(一个ImageVIEw和两个TextVIEw)的相对位置,下图是使用HIErarchyVIEwer抓获的布局剪影:

实现这个布局,直接使用linearLayout就可以了。列表项本身是一个水平的linearLayout,里面有一个ImageVIEw和一个垂直的linearLayout,垂直的linearLayout里包含两个TextVIEw。以下是这个布局的源代码:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="?androID:attr/ListPreferredItemHeight"  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="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="?androID:attr/ListPreferredItemHeight"  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" /><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的对齐锚点。在我们的这个场合,使用alignWithParentIfMissing的结果是relativeLayout将标题部分的底部与自己的底部对齐。结果如下所示:

现在,我们的布局表现得很完美了,即使描述部分的Visibility属性设为GONE。更好的是,层级更加简单,因为我们不再使用linearLayout,而且,更加高效了。当我们使用HIErarchyVIEwer来比较两个实现版的时候,事实就更明显了:

另外,当你使用这么一个布局作为ListVIEw的列表项时,这种差异就更为重要了。希望这个简单的例子能让你了解布局,了解如何优化你的UI。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android布局技巧之创建高效布局全部内容,希望文章能够帮你解决Android布局技巧之创建高效布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1149177.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存