Android Layout Tricks #1--因为空间被墙,所以转载过来
来源:互联网 发布:多听fm无法连接网络 编辑:程序博客网 时间:2024/06/01 08:25
原文:
http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
Android Layout Tricks #1
The Android UI toolkit offers several layout managers that are rather easy to use and, most of the time, you only need the basic features of these layout managers to implement a user interface. Sticking to the basic features is unfortunately not the most efficient way to create user interfaces. A common example is the abuse of LinearLayout, which leads to a proliferation of views in the view hierarchy. Every view, or worse every layout manager, you add to your application comes at a cost: initialization, layout and drawing become slower. The layout pass can be especially expensive when you nest several LinearLayout that use the weight parameter, which requires the child to be measured twice.
Let’s consider a very simple and common example of a layout: a list item with an icon on the left, a title at the top and an optional description underneath the title. Here is what such an item looks like:
To clearly understand how the views, one ImageView and two TexView, are positioned with respect to each other, here is the wireframe of the layout as captured byHierarchyViewer:
Implementing this layout is straightforward with LinearLayout. The item itself is a horizontal LinearLayout with an ImageView and a vertical LinearLayout, which contains the two TextViews. The source code of this layout is the following:
<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>
This layout works but can be wasteful if you instantiate it for every list item of aListView. The same layout can be rewritten using a singleRelativeLayout, thus saving one view, and even better one level in view hierarchy, per list item. The implementation of the layout with a RelativeLayout remains simple:
<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>
This new implementation behaves exactly the same way as the previous implementation, except in one case. The list item we want to display has two lines of text: the title and anoptional description. When a description is not available for a given list item, the application would simply set the visibility of the second TextView toGONE. This works perfectly with the LinearLayout implementation but not with the RelativeLayout version:
In a RelativeLayout, views are aligned either with their parent, the RelativeLayout itself, or other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout and that the title is positioned above the description and anchored to the parent’s top. With the description GONE, RelativeLayout doesn’t know where to position the title’s bottom edge. To solve this problem, you can use a very special layout parameter calledalignWithParentIfMissing.
This boolean parameter simply tells RelativeLayout to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing to true, RelativeLayout will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing will cause RelativeLayout to align the title’s bottom with its own bottom. The result is the following:
The behavior of our layout is now perfect, even when the description is GONE. Even better, the hierarchy is simpler and because we are not using LinearLayout’s weights it’s also more efficient. The difference between the two implementations becomes obvious when comparing the view hierarchies in HierarchyViewer:
Again, the difference will be much more important when you use such a layout for every item in a ListView for instance. Hopefully this simple example showed you that getting to know your layouts is the best way to learn how to optimize your UI.
- Android Layout Tricks #1--因为空间被墙,所以转载过来
- 因为空间被墙,所以转载进来
- Android Layout Tricks #1
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #1(Android 布局技巧1)
- 因为迁移,所以出卖(1)
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize with stubs
- Android Layout Tricks #4: Optimize, Part 2
- android layout 研究 转载
- 因为爱, 所以爱
- 因为得到,所以放弃
- 因为懂得,所以慈悲
- 因为爱所以爱
- 因为寂寞 所以抽烟
- 因为更新,所以更新
- CLR via C#共享程序集
- Arm-linux-gcc-4.3.2安装步骤
- boost下circular_buffer的速度问题
- Asp.net里实现分页浏览方法大总结
- java基础问题---java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用
- Android Layout Tricks #1--因为空间被墙,所以转载过来
- 程序员是否需要学习底层知识
- Win XP系统环境变量对应路径说明
- Android编程示例之——UI主线程以及多线程
- 理解虚基类、虚函数与纯虚函数的概念
- Chrome浏览器Windows下快捷键大全
- Android重量级开发之--提高android启动速度研究
- C# 连接EXCEL/2003/2007/2010 和ACCESS/2003/2007/2010
- secureCRT通过代理进行ssh连接