上一篇文章中讲到了Merge的用法和注意事项,所以本人就想,做了一个Demo进行验证下。

一、Merge的使用

(1)activity中的onCreate方法中的setContentView(R.layout.main2);

(2)应用Include或者ViewStub标签从外部导入xml结构时,可以将被导入的xml用merge作为根节点表示,这样当被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点。<include layout="@layout/main2"/>

(3)当需要扩充的xml layout本身是由merge作为根节点的话,需要将被导入的xml layout置于 viewGroup中,同时需要设置attachToRoot为True。

View view = inflater.inflate(R.layout.main2, container, true);【注意,Fragment的onCreateView方法中不能使用这个方法,因为ViewGroup container为空】

 二、Merge的注意事项

(1)<merge />只可以作为xml layout的根节点。

(2)如果你所创建的xml layout并不是用FramLayout作为根节点(而是应用LinerLayout等定义root标签),就不能应用上边的例子通过merge来优化UI结构。【merge的布局效果跟FrameLayout是等同的】

(3)Merge的父布局最好也是FrameLayout,因为使用Merge优化是指将<merge />内的元素添加到<merge />的父元素里,如果父布局不是FrameLayout,那么merge的元素添加到父布局中后,本来的展现效果就是发生变化——按照父布局的样式进行展现。

三、验证

MergeDemo的目录如下图所示:

3.1、最初的布局【activity_main.xml】

(1)布局文件

<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.why.mergedemo.activity.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_main" /> </RelativeLayout>

activity_main

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.2、用法一:activity中的onCreate方法中的setContentView(R.layout.main2);

3.2.1、activity_oncreate_linear

(1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="activity_oncreate_linear"
android:textColor="#ffffffff" /> </LinearLayout>

activity_oncreate_linear

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oncreate_linear);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.2.2、activity_oncreate_frame

(1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="activity_oncreate_frame"
android:textColor="#ffffffff" /> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="activity_oncreate_frame"
android:textColor="#ffffffff" /> </FrameLayout>

activity_oncreate_frame

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oncreate_frame);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.2.3、activity_oncreate_merge

(1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/goldenIv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="activity_oncreate_merge"
android:textColor="#ffffffff" /> </merge>

activity_oncreate_merge

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oncreate_merge);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.3、用法二:应用Include从外部导入xml结构时

3.3.1、activity_include_frame

(1)布局文件

<LinearLayout 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"
tools:context="com.why.mergedemo.activity.MainActivity" > <TextView
android:id="@+id/helloId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_include_frame" /> <include
android:id="@+id/includeId"
layout="@layout/include_frame"/> </LinearLayout>

activity_include_frame

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="include_frame"
android:textColor="#ffffffff" /> </FrameLayout>

include_frame

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_include_frame);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.3.2、activity_include_merge

(1)布局文件

<LinearLayout 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"
tools:context="com.why.mergedemo.activity.MainActivity" > <TextView
android:id="@+id/helloId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_include_merge" /> <include
android:id="@+id/includeId"
layout="@layout/include_merge"/> </LinearLayout>

activity_include_merge

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="include_merge"
android:textColor="#ffffffff" /> </merge>

include_merge

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_include_merge);
} }

MainActivity

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.4、用法三:应用inflate从外部导入xml结构时

3.4.1、activity_inflater_customlayout(framelayout--false)

(1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/helloId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_inflater_customlayout" /> <com.why.mergedemo.custom.CustomLinearLayout
android:id="@+id/customLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/helloId"
android:orientation="vertical"> </com.why.mergedemo.custom.CustomLinearLayout> </RelativeLayout>

activity_inflater_customlayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="include_frame"
android:textColor="#ffffffff" /> </FrameLayout>

include_frame

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inflater_customlayout);
} }

MainActivity

package com.why.mergedemo.custom;

import com.why.mergedemo.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout; public class CustomLinearLayout extends LinearLayout{ public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initCustonView(context);
} /*
* inflate(int resource, ViewGroup root, boolean attachToRoot)
* 1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。
* 2. 如果root不为null,attachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。
* 3. 如果root不为null,attachToRoot设为false,则root参数失去作用。
* 4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。*/ public void initCustonView(Context context){
//引入R.layout.include_frame, attachToRoot不能等于true【Caused by: java.lang.StackOverflowError】
//引入R.layout.include_merge, 不能有this.addView(view);
//View view = LayoutInflater.from(context).inflate(R.layout.include_merge, this, true); View view = LayoutInflater.from(context).inflate(R.layout.include_frame, this, false);
this.addView(view);
} }

CustomLinearLayout

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

3.4.2、activity_inflater_customlayout(merge--true)

(1)布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/helloId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_inflater_customlayout" /> <com.why.mergedemo.custom.CustomLinearLayout
android:id="@+id/customLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/helloId"
android:orientation="vertical"> </com.why.mergedemo.custom.CustomLinearLayout> </RelativeLayout>

activity_inflater_customlayout

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/golden_gate" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dip"
android:background="#AA000000"
android:padding="12dip"
android:text="include_merge"
android:textColor="#ffffffff" /> </merge>

include_merge

(2)activity类

package com.why.mergedemo.activity;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inflater_customlayout);
} }

MainActivity

package com.why.mergedemo.custom;

import com.why.mergedemo.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout; public class CustomLinearLayout extends LinearLayout{ public CustomLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initCustonView(context);
} /*
* inflate(int resource, ViewGroup root, boolean attachToRoot)
* 1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。
* 2. 如果root不为null,attachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。
* 3. 如果root不为null,attachToRoot设为false,则root参数失去作用。
* 4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。*/ public void initCustonView(Context context){
//引入R.layout.include_frame, attachToRoot不能等于true【Caused by: java.lang.StackOverflowError】
//引入R.layout.include_merge, 不能有this.addView(view);
View view = LayoutInflater.from(context).inflate(R.layout.include_merge, this, true); /*View view = LayoutInflater.from(context).inflate(R.layout.include_frame, this, false);
this.addView(view);*/
} }

CustomLinearLayout

(3)HierarchyViewer图(局部)【最左侧的FrameLayout就是指MainActivity的默认布局】

(4)效果图

关于Merge的整理--Merge的使用方法和注意事项的Demo的更多相关文章

  1. 【hibernate merge】session1.merge(T entity)方法的含义和update方法的区别

    注意:  MERGE语句是SQL语句的一种.在SQL Server.Oracle数据库中可用,MySQL.PostgreSQL中不可用. 1>session1.merge(T entity) 合 ...

  2. Git - Merge: refusing to merge unrelated histories

    场景 我在本地有个代码仓库local-A,本地仓库local-A已经和一个远程仓库remote-A关联了. 接着我又在GitHub上新建了一个仓库remote-B,我希望将本地仓库local-A的本地 ...

  3. Linux任务调度进程crontab的使用方法和注意事项

    参考文章:Linux任务调度进程crond命令的使用方法和注意事项 一.crond简介 概念 crond的概念和crontab是不可分割的.crontab是一个命令,常见于Unix和类Unix的操作系 ...

  4. Swift基础--Swift中的分类以及在分类中扩展init方法的注意事项

    Swift中的分类 1.创建一个空的swift文件 2.关键字extension,格式: extension 要扩展的类名 {} extension UIButton { } Swift中扩展init ...

  5. C++ 开发OCX 的方法和注意事项

    C++ 开发OCX 的方法和注意事项 前言 ActiveX控件是一种实现了一系列特定接口而使其在使用和外观上更象一个控件的COM组件.ActiveX控件这种技术涉及到了几乎所有的COM和OLE的技术精 ...

  6. java字符流操作flush()方法及其注意事项

    java字符流操作flush()方法及其注意事项   flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...

  7. Java编程中获取键盘输入实现方法及注意事项

    Java编程中获取键盘输入实现方法及注意事项 1. 键盘输入一个数组 package com.wen201807.sort; import java.util.Scanner; public clas ...

  8. Java中使用方法的注意事项

    Java方法使用的注意事项 本文列举了几个小白在java中使用方法应该注意的几个地方 1. 方法应该定义在类中2.方法中不可以再嵌套方法3.方法定义的前后顺序无所谓4.想要执行方法必须要调用5.如果方 ...

  9. Java基础进阶:继承重点摘要,继承详解,方法重写注意事项,方法重载与重写的区别,抽象类,代码块, 附重难点,代码实现源码,课堂笔记,课后扩展及答案

    继承重点摘要 *继承的特点: 子类在初始化之前,一定要先完成父类数据的初始化 子类在初始化之前,一定要先访问父类构造,完成父类数据的初始化 系统在每一个构造方法中默认隐藏了一句super(); 如果我 ...

随机推荐

  1. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

  2. 栈的简单应用 HDU 1022 http://acm.hdu.edu.cn/showproblem.php?pid=1022

    #include<stdio.h> #include<stack> #include<string.h> #define N 20 using namespace ...

  3. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

  4. iOS应用内HTTP服务上传文件

    相信很多朋友都用过AirAV.100tv这类iOS视频播放应用中通过Wifi,从PC上输入Web地址上传文件到iOS设备上,我也一直想实现这个功能,苦于知识掌握有限,后来在其他群友的指导下参照很多大神 ...

  5. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  6. oracle 中控制文件中到底记录了哪些信息

     oracle 控制文件中的信息  oracle 11g                                             oracle 10g   DATABASE       ...

  7. 存储过程[st_MES_RptInspectShipment]

    USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_RptInspectShipment] Script Date: ...

  8. RealSense 3D实感体验:前景广阔目前应用少

    腾讯数码讯(周硕)在去年的IDF大会上,英特尔着重展示了其全新的RealSense 3D实感技术,而厚度仅6mm堪称史上最薄平板的戴尔Venue 8 7000也成为首个搭载RealSense技术的产品 ...

  9. 【不积跬步,无以致千里】关闭631端口cups打印服务和8009端口ajp

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  10. 将Java程序作成exe文件的几种方法【转载】

    看到网上有同志的介绍将Java程序作成exe文件的方法,写的不错,但是也许是这篇文章完成的时间比较早,许多内容已经不合适了.我在这里补充几条: 一.exe4j 说明:exe4j可以将Jar文件制作成e ...