Android Layout Tricks #3: Optimize by merging(Android 布局技巧3:使用merge优化)
来源:互联网 发布:淘宝充值代理平台 编辑:程序博客网 时间:2024/05/22 04:53
03 March 2009
Android Layout Tricks #3: Optimize by merging
In the previous installment of Android Layout Tricks, I showed you how to use the <include />
tag in XML layout to reuse and share your layout code. I also mentioned the<merge />
and it's now time to learn how to use it.
The <merge />
was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees. It's easier to understand the problem this tag solves by looking at an example. The following XML layout declares a layout that shows an image with its title on top of it. The structure is fairly simple; aFrameLayout is used to stack a TextView on top of an ImageView:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/golden_gate" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:layout_gravity="center_horizontal|bottom" android:padding="12dip" android:background="#AA000000" android:textColor="#ffffffff" android:text="Golden Gate" /></FrameLayout>
This layout renders nicely as we expected and nothing seems wrong with this layout:
Things get more interesting when you inspect the result with HierarchyViewer. If you look closely at the resulting tree you will notice that theFrameLayout
defined in our XML file (highlighted in blue below) is the sole child of anotherFrameLayout
:
Since our FrameLayout
has the same dimension as its parent, by the virtue of using thefill_parent
constraints, and does not define any background, extra padding or a gravity, it istotally useless. We only made the UI more complex for no good reason. But how could we get rid of thisFrameLayout
? After all, XML documents require a root tag and tags in XML layouts always represent view instances.
That's where the <merge />
tag comes in handy. When the LayoutInflater encounters this tag, it skips it and adds the <merge />
children to the<merge />
parent. Confused? Let's rewrite our previous XML layout by replacing theFrameLayout
with <merge />
:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/golden_gate" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:layout_gravity="center_horizontal|bottom" android:padding="12dip" android:background="#AA000000" android:textColor="#ffffffff" android:text="Golden Gate" /></merge>
With this new version, both the TextView
and the ImageView
will be added directly to the top-levelFrameLayout
. The result will be visually the same but the view hierarchy is simpler:
Obviously, using <merge />
works in this case because the parent of an activity's content view is always aFrameLayout
. You could not apply this trick if your layout was using aLinearLayout
as its root tag for instance. The <merge />
can be useful in other situations though. For instance, it works perfectly when combined with the<include />
tag. You can also use <merge />
when you create a custom composite view. Let's see how we can use this tag to create a new view calledOkCancelBar
which simply shows two buttons with customizable labels. You can alsodownload the complete source code of this example. Here is the XML used to display this custom view on top of an image:
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/golden_gate" /> <com.example.android.merge.OkCancelBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingTop="8dip" android:gravity="center_horizontal" android:background="#AA000000" okCancelBar:okLabel="Save" okCancelBar:cancelLabel="Don't save" /></merge>
This new layout produces the following result on a device:
The source code of OkCancelBar
is very simple because the two buttons are defined in an external XML file, loaded using aLayoutInflate
. As you can see in the following snippet, the XML layoutR.layout.okcancelbar
is inflated with the OkCancelBar
as the parent:
public class OkCancelBar extends LinearLayout { public OkCancelBar(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(HORIZONTAL); setGravity(Gravity.CENTER); setWeightSum(1.0f); LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0); String text = array.getString(R.styleable.OkCancelBar_okLabel); if (text == null) text = "Ok"; ((Button) findViewById(R.id.okcancelbar_ok)).setText(text); text = array.getString(R.styleable.OkCancelBar_cancelLabel); if (text == null) text = "Cancel"; ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text); array.recycle(); }}
The two buttons are defined in the following XML layout. As you can see, we use the<merge />
tag to add the two buttons directly to the OkCancelBar
. Each button is included from the same external XML layout file to make them easier to maintain; we simply override their id:
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_ok" /> <include layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_cancel" /></merge>
We have created a flexible and easy to maintain custom view that generates an efficient view hierarchy:
The <merge />
tag is extremely useful and can do wonders in your code. However, it suffers from a couple of limitation:
<merge />
can only be used as the root tag of an XML layout- When inflating a layout starting with a
<merge />
, you must specify a parentViewGroup
and you must setattachToRoot
totrue
(see the documentation of theinflate() method)
In the next installment of Android Layout Tricks you will learn aboutViewStub
, a powerful variation of <include />
that can help you further optimize your layouts without sacrificing features.
Download the complete source code of this example.
- Android Layout Tricks #3: Optimize by merging(Android 布局技巧3:使用merge优化)
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize by merging
- Android Layout Tricks #3: Optimize with stubs(Android 布局技巧3:使用stub优化)
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #3: Optimize with stubs
- Android Layout Tricks #3: Optimize, Part 1
- Android Layout Tricks #1(Android 布局技巧1)
- Android Layout Tricks #4: Optimize, Part 2
- Android Layout Tricks #2: Reusing layouts(Android 布局技巧2:重用布局)
- Android 布局优化(merge使用)
- Android布局优化技巧--<include>/<merge>/<viewStub>
- Android UI 优化 [ 类别:Layout ] #3 merge用法 -android
- Android merge布局优化
- android布局优化-merge
- android Merge的使用(布局的优化)
- Android布局优化 merge标签使用
- Android 布局优化merge标签的使用
- 免费申请邓白氏编码
- js按钮改成连接跳转
- asp.net导出excle表
- SELECT时的with(nolock)选项说明
- iphone程序国际化(本地化)
- Android Layout Tricks #3: Optimize by merging(Android 布局技巧3:使用merge优化)
- UIApplication sharedApplication
- 英语飙升的好方法
- 老紫竹的华为面试之旅
- C# datagridview 导出EXCEL 类
- MIME type
- 你需要知道的 16 个 Linux 服务器监控命令
- WIP_ENTITIES.ENTITY_TYPE
- Twitter走在十字路口:从言论平台转向广告媒体引发争议