Android UI 优化 [ 类别:Layout ] #1
来源:互联网 发布:淘宝api接口 sdk 编辑:程序博客网 时间:2024/05/17 10:42
有一句古话:不论黑猫白猫,能抓到耗子就是好猫。这个也许在某些方面是有道理的,但对于我们追求精益求精的思想是背道而驰的,往往就是因为满足于一个结果,而放弃探求更加优化的处理方法。
当关注应用程序或者游戏所达到的结果时,往往非常容易忽视一些优化的问题,例如内存优化,线程优化,Media优化和UI优化等等。不同的模块都存在更为巧妙的方式来对待一般性问题,所以每当我们实现一个行为后,稍微多花一些时间来考虑目前所作的工作是否存在更为高效的解决办法。
这一次我们对常用的UI Layout优化说起,这个例子转载于 android developing blog
在Android中最常用LinearLayout表示UI的框架,而且也是最直观和方便的方法。例如创建一个UI用于展现Item的基本内容。如图所示:
线框图:
直接可以通过LinearLayout快速的实现这个UI的排列:
<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>
尽管可以通过Linearlayout实现我们所预想的结果,但是在这里存在一个优化的问题,尤其是针对为大量Items。比较RelativeLayout和LinearLayout,在资源利用上前者会占用更少的资源而达到相同的效果,以下是用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>
针对RelativeLayout有一点需要注意,因为它内部是通过多个View之间的关系而确定的框架,那么当其中某一个View因为某些需要调用GONE来完全隐藏掉后,会影响与其相关联的Views。Android为我们提供了一个属性alignWithParentIfMissing 用于解决类似问题,当某一个View无法找到与其相关联的Views后将依据alignWithParentIfMissing的设定判断是否与父级View对齐。
下边是两种不同layout在Hierarchy Viewer中的层级关系图:
- Android UI 优化 [ 类别:Layout ] #1
- Android UI 优化 [ 类别:Layout ] #1
- Android UI 优化 [ 类别:Layout ] #2
- Android UI 优化 [ 类别:Layout ] #3
- Android UI 优化 [ 类别:Layout ] #3 merge用法 -android
- Android UI 优化 [ 类别:Layout ] #2 viewStub include requestFocus
- Android UI 优化 [ 类别:Layout ] <merge>的学习
- 如何优化Android layout(UI)性能
- UI layout 优化
- Android : UI Layout
- Android UI 优化 1
- [Android Develop_002]-Android UI Layout
- Android layout优化
- Android Layout 优化
- Android UI Layout 之 AbsoluteLayout
- Android UI Layout 之 AbsoluteLayout
- Android基本之UI Layout
- android UI-Layout界面布局
- java Logger 的使用与配置2
- Android UI 优化 [ 类别:Layout ] #2 viewStub include requestFocus
- 学习opencv ,图像分割中分水岭算法的感性认识及cvWatershed例子
- 使IE6下PNG背景透明的七种方法任你选
- C#中对象的序列化与反序列化
- Android UI 优化 [ 类别:Layout ] #1
- 通信工具的音视频的网络传输实现
- Struts 1 区别 Struts2
- ASP.NET MVC Framework 系列
- poj 3352 (构造双连通分量)
- 保证都是你没有体验过的
- 导入文本文件时如何指定字段类型
- Peer-to-Peer Botnets P2P僵尸网络---Ping Wang, Baber Aslam, and Clif C. Zou
- java反射机制详解 及 Method.invoke解释