Android中include和Merge节点分析
2016-07-01 13:31
295 查看
大家可能都会遇见这样一种情况,开发apk时顶部标题或者底部标题好多activity都相同,如果你每个activity的布局界面都去添加代码,一是会使代码冗余而且无用,二是会让别人觉得你编程水平不行,关键是需要不断地去重复一样的代码在我看来对编程水平的提高没有任何帮助,而且只会更累浪费时间。有一个很重要的衡量编程能力的指标,那就是看你代码的复用性,这是后就要想到include节点,是对UI布局进行复用。
为了编写标题复用代码,我们使用include,于是我们可以这样写:
在activity布局xml文件中添加include节点
然后在include节点下引用一个layout文件
于是,代码复用已经做好了,但可以看到,activity的布局文件外层有个RelativeLayout,include节点又引用RelativeLayout布局,虽然很好的实现了代码的复用,但这无疑会影响UI的加载性能,我们想要的是include节点下的布局可以直接嵌入到include的外层节点,而不是又多一层布局。Merge就很好的解决了这个问题
include引用该xml文件就相当于直接嵌入到include的外层布局,忽略掉merge节点,将两个textView直接加入到布局,也就是相当于activity的布局如下
使用merge+include,既实现了代码复用,又解决了布局层级问题,在简化代码的基础上提升UI布局加载。
分析完成后可以看出include和merge根本不在一个level,也没有任何的可比性,这一点从源码可以看出
Merge和include的定义如下
可以看到,merge的布局只是一个linearlayout,而include布局则是一个xml文件,所以include可以引用别的layout文件,而merge相当于linearlayout节点,两者根本就不是一个东西,所以也不要再进行任何比较。
总结,本文只是对代码复用举了一个小例子,在实际开发中要注意不仅仅是复用xml文件,activity和整个fragment等都可以复用,作为一名研发人员,在开发apk时不仅要注重功能实现,还要注重代码的设计,可读性,等等,这样才算得上是研发人员,而不是只懂得copy&paste的搬运工
为了编写标题复用代码,我们使用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的搬运工
相关文章推荐
- android view各属性详解之onDraw
- 浅析android应用增量升级
- Android中ANR分析
- 手机浏览器页面点击不跳转(Android手机部分浏览器) 浏览器双击放大网页 解决
- Android 代码中设置控件的style
- Android中的坐标系以及获取坐标的方法
- Android白话启动篇(Android booting process)
- Android AnimationDrawable运行的几种方式
- android侧滑菜单笔记
- Android之四大组件之一-Activity(二)
- 熟悉Android 画图
- android 禁用和开启四大组件的方法
- Android Service组件开发用到的几个知识点
- Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
- android tint selector 使用
- Java,Android回调机制精讲
- Android 双曲线波浪动画(第一发)
- android-----事件分发机制测试系列(三)
- Android 代码中动态为RadioGroup添加RadioButton
- Android之四大组件之一-Activity(一)