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

Android中include和Merge节点分析

2016-07-01 13:31 295 查看
大家可能都会遇见这样一种情况,开发apk时顶部标题或者底部标题好多activity都相同,如果你每个activity的布局界面都去添加代码,一是会使代码冗余而且无用,二是会让别人觉得你编程水平不行,关键是需要不断地去重复一样的代码在我看来对编程水平的提高没有任何帮助,而且只会更累浪费时间。有一个很重要的衡量编程能力的指标,那就是看你代码的复用性,这是后就要想到include节点,是对UI布局进行复用。

为了编写标题复用代码,我们使用include,于是我们可以这样写:

在activity布局xml文件中添加include节点

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.fang.zrf.bleoppdemo.MainActivity">

<include
android:id="@+id/header"
layout="@layout/include"
android:layout_width="match_parent"
android:layout_height="wrap_parent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>


然后在include节点下引用一个layout文件

于是,代码复用已经做好了,但可以看到,activity的布局文件外层有个RelativeLayout,include节点又引用RelativeLayout布局,虽然很好的实现了代码的复用,但这无疑会影响UI的加载性能,我们想要的是include节点下的布局可以直接嵌入到include的外层节点,而不是又多一层布局。Merge就很好的解决了这个问题

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</merge>


include引用该xml文件就相当于直接嵌入到include的外层布局,忽略掉merge节点,将两个textView直接加入到布局,也就是相当于activity的布局如下

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.fang.zrf.bleoppdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

 </RelativeLayout>



使用merge+include,既实现了代码复用,又解决了布局层级问题,在简化代码的基础上提升UI布局加载。

分析完成后可以看出include和merge根本不在一个level,也没有任何的可比性,这一点从源码可以看出

Merge和include的定义如下

public class Merge extends Activity {
private LinearLayout mLayout;

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(this).inflate(R.layout.merge_tag, mLayout);

setContentView(mLayout);
}

public ViewGroup getLayout() {
return mLayout;
}
}


public class Include extends Activity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.include_tag);
}
}


可以看到,merge的布局只是一个linearlayout,而include布局则是一个xml文件,所以include可以引用别的layout文件,而merge相当于linearlayout节点,两者根本就不是一个东西,所以也不要再进行任何比较。

总结,本文只是对代码复用举了一个小例子,在实际开发中要注意不仅仅是复用xml文件,activity和整个fragment等都可以复用,作为一名研发人员,在开发apk时不仅要注重功能实现,还要注重代码的设计,可读性,等等,这样才算得上是研发人员,而不是只懂得copy&paste的搬运工
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: