您的位置:首页 > 移动开发 > Android开发

android merge与include标签混合使用

2014-12-24 17:14 519 查看
前面一篇博客讲了merge标签的使用场景以及用法,这篇就讲一下include的使用,但是include标签说起来比较简单,所以就结合起merge标签

一起来讲。

merge标签之前说过了,所以就不说了。include标签的作用在于使得布局文件变得可复用,比如你在不同的布局文件中要加入相同的按钮或者

title时,那么这时候就可以在另外一个布局文件中设置好这个按钮或者title,然后使用include标签将这个设置好的布局文件插入到要添加的布局

文件中,这样就达到了布局的可复用。

用一个例子说明:

我要在一个布局文件中添加两个按钮,那么我可以在另外一个文件中先定义好这个按钮,然后使用include将这个文件插入进去

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_horizontal"
android:id="@+id/test">

</Button>


按钮xml文件定义好了之后,接下来就是使用include标签将这个xml布局文件插入到另一个布局文件中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
layout="@layout/save_cancel_bar_button"
android:id="@+id/save" />

<include
layout="@layout/save_cancel_bar_button"
android:id="@+id/cancel" />
</LinearLayout>


定义好了之后就能够根据id来获取button控件了。

以上就是include的使用方法。

接下来我们就来讲include标签和merge标签的结合使用

我们知道merge标签是layout文件的根元素,而include标签的作用复用layout布局文件,那么当include标签的父layout和其包含的layout的布局是

相同的,那么就可以使用merge进行优化了。下面就通过例子来说明include和merge的混合使用。

定义一个Activity xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="@drawable/title"
android:id="@+id/title">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#e8f3fb"
android:textSize="22sp"
android:text="设置"/>
</RelativeLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title">

<include
layout="@layout/picture_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

</RelativeLayout>
效果图如下:



从上面的xml文件代码可以看出,他使用include标签引用了一个布局文件picture_scanner

<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:save_cancel_bar="http://schemas.android.com/apk/res/com.lonuery.merge"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/guide_map1"/>

<com.lonuery.merge.Save_Cancel_Bar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="8dip"
android:gravity="center_horizontal"
android:background="#AA000000"
save_cancel_bar:savebarlabel="df"
save_cancel_bar:cancelbarlabel="fd">
</com.lonuery.merge.Save_Cancel_Bar>

</merge>


然后在这个布局文件中定义好控件位置,然后我们可以通过hierarchyviewer来查看其布局节点构成



可以看到在include所引入的xml文件中使用merge标签时,ImageView和Save_Cancel_Bar和FrameLayout之间没有间接节点,但是如果不使用merge

标签呢,改成FrameLayout看下效果

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:save_cancel_bar="http://schemas.android.com/apk/res/com.lonuery.merge"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/guide_map1"/>

<com.lonuery.merge.Save_Cancel_Bar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="8dip"
android:gravity="center_horizontal"
android:background="#AA000000"
save_cancel_bar:savebarlabel="df"
save_cancel_bar:cancelbarlabel="fd">
</com.lonuery.merge.Save_Cancel_Bar>

</FrameLayout>


使用hierarchyviewer看下效果



我们可以看见ImageView和Save_Cancel_Bar在和之前哪一个FrameLayout之间多出了一个FrameLayout的节点,而且他是前面一个FrameLayout的唯

一子节点,所以这个FrameLayout是可以通过merge优化掉的。

这样include和merge标签的结合使用就讲完了。通过上面hierarchyviewer的图可以看出,这个图还可以有优化的空间,因为Save_Cancel_Bar后面只有

一个唯一子节点LinearLayout,那么这个LinearLayout是可以优化掉的。好我们就来看Save_Cancel_Bar自定义控件的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>


我们直接将LinearLayout替换为merge后会发现,在运行界面的布局会变形,那么我们在使用merge标签后使得布局不变形,看能不能够达到优化的目的。

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
layout="@layout/save_cancel_bar_button"
android:id="@+id/save" />

<include
layout="@layout/save_cancel_bar_button"
android:id="@+id/cancel" />
</merge>


在这里我又使用了一个include标签,因为我发现这两个button可以使用同一个布局文件

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center_horizontal"
android:id="@+id/test">

</Button>


通过hierarchyviewer看下优化后的图:



通过上面图片我们可以看出,在Button和Save_Cancel_Bar直接的LinearLayout的节点被优化掉了。这样这个布局文件就达到最优的性能了

在上面优化的过程中我总结了merge优化的几个case:

1、merge的父布局可以不和merge之前替换的布局相同,也就是说,你使用merge替换掉了LinearLayout,然后将这个merge xml文件放在

RelativeLayout中也行,只要布局不变形,并不是你使用merge替换掉LinearLayout后,这个merge xml就必须放在LinearLayout布局下。

2、假如merge标签替换掉LinearLayout,然后将merge xml文件放在FrameLayout中,那么merge布局文件中的子控件所依赖的父布局就是

FrameLayout这也是为什么1中的情况会变形,因为merge xml中的控件的父布局右LinearLayout 变成了RelativeLayout。

3、在自定义控件中,如我这个例子,Save_Cancel_Bar继承了LinearLayout,那么这个控件就相当于一个LinearLayout,那么其布局文件

中的LinearLayout就是其一个子节点,那么这个LinearLayout按照逻辑来说就可以优化掉,但是如果我使用merge标签替换掉其布局文件中

的LinearLayout,这个布局文件会变形,可见merge标签所导致布局依赖只在xml中有效,对于使用代码定义的布局不起作用。

源代码下载:http://download.csdn.net/detail/zkw12358/8295667
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: