<include />&<merge />

本文解释了Android布局文件中merge标签的用途,通过实例展示了如何利用merge标签精简视图层级,提高布局效率。


上周参加中兴移动的招聘会,完全被面试官强烈的气场给震慑住了。使我明白我只是一个android刚入门的菜鸟,还有很多东西需要学习。其中有个面试官问了一个问题:merge标签的作用是什么?我愣是没有回答上来。今天有点时间就看看它到底是什么。

还是先阅读以下android开发文档。

根据官方的描述,当在一个layoutA文件中使用<include />引用另一个layoutB文件时,可以在layoutB中使用<merge />标签来替代该文件视图阶层中多余的视图组(view groups)。

举个例子,

如下layoutA文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/
layoutB"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

</LinearLayout>

如下layoutB文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</LinearLayout>

layoutA的文件最终会是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</LinearLayout>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

</LinearLayout>

其实上述红色的部分的代码是多余的。而将layoutB修改为如下时:

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

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

</merge>

layoutA的最终代码将会是如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">


    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/add"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/delete"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

</LinearLayout>

那么就不会出现多余的viewgroups了,而<merge />也会在视图阶层中消失。这样视图阶层会变得简单,加载起来也更加有效率。

 

注意一点,当在代码中直接inflate<merge />为根标签的layout文件时,inflate的第二个参数不能为空,并且第三个参数必须为true。因为在inflate方法的源码中有如下的一段代码:

if (TAG_MERGE.equals(name)) {

                   if (root == null || !attachToRoot) {

                       throw new InflateException("<merge /> canbe used only with a valid "

                                + "ViewGroup root andattachToRoot=true");

                    }

......

最近,小明同学学习了泛型编程技术——模板,了解了函数模板与类模板的设计,并且设计了不少基于模板技术实现的函数和类,你特别羡慕小明掌握了这些编程的技术,于是你用了几顿饭的代价,从小明那里学到了相关的知识,并想尝试一下。数学上,我们学过集合(元素不重复),结合标准模板库,我们可以一个动态有序数组来表示集合,并能动态调整集合的大小(容量)。经过前期的实验,你基本熟悉了相关知识,并已经设计好了类模板框架,设计了几个简单的成员函数,如各种构造函数、插入、查找、输出、赋值等。在此基础上,我们也希望小明能实现集合的相关运算,现在请你写一个函数,可以求两个集合的并集。 类模板及接口定义: template<typename T> class MySet{ T* pset; int cur_size, size; //cur_size当前元素个数,size表示集合的最大容量 const int incre = 10; //集合为满时,动态调整容量的增量 public: MySet(int size=10); //构造一个长度为size的空集,已实现 MySet(T* p, int n); //构造函数,用一个长度为n的数组对集合初始化,已实现 MySet(const MySet<T>& ms); //复制构造函数,已实现 ~MySet(); //析构函数,已实现 MySet<T>& operator=(const MySet<T>& ms); //赋值运算符,已实现 MySet<t>& merge(const MySet<T>& ms); //将另一个集合合并过来,已实现 int length() const; //返回集合当前元素个数,已实现 int capacity () const; //返回集合的容量,已实现 bool empty() const; //判断是否为空集,已实现 int find(T e); //查找,已实现 MySet<T>& insert(T e); //向集合中插入一个元素,已实现 void print() const; //输出集合中的元素,已实现 }; template<typename T> MySet<T> UnionSet(const MySet<T>&, const MySet<T>&);///求两个集合的并集 裁判测试程序样例: #include <iostream> #include <string> using namespace std; int main(){ int n1, n2; cin >> n1 >> n2; int a1[n1], a2[n2]; for(int i=0; i<n1; i++) cin >> a1[i]; for(int i=0; i<n2; i++) cin >> a2[i]; MySet<int> ms1(a1, n1); MySet<int> ms2(a2, n2); MySet<int> msc; msc = UnionSet(msc, msc1); msc.print(); msc = UnionSet(msc1, ms2); msc.print(); return 0; } /* 请在这里填写答案,类模板的完整实现已由系统给出,你只要实现函数UnionSet即可 */ 输入样例: 3 5 1 2 3 2 3 4 5 6 输出样例: 1 2 3 1 2 3 4 5 6 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值