在Android布局文件中,某些时候使用include标签会很多的好处
1,对于稍微有点复杂的布局界面,将所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差,这时可以分开使用include标签来处理
2,当Activity需要用到同样的布局效果,也可以使用include标签处理,而不用把一样的布局代码重复拷贝几遍,不用以后修改起来每个地方都要修改,提高了代码的重用性
我们先用include标签实现下面的效果

activity_main.xml

<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"
tools:context=".MainActivity" > <TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="include使用方法"
android:gravity="center"
/> <include
layout="@layout/layout_sec1"
android:id="@+id/layout_sec1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
/> <include
layout="@layout/layout_sec2"
android:id="@+id/layout_sec2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_sec1"
/> <include
layout="@layout/layout_sec3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

可以看到在这个布局文件中使用了3个include标签对应3个layout,调用了Include之后,对应的细分布局文件内容就被完全嵌入到了include所指定的位置.这3个layout的代码就不贴出来了.
此程序只有布局代码,没有java代码,比较简单,可以自己实现或者下载源码看下
这里主要要注意的是,经常有人在RelativeLayout中使用include标签
但是却发现include进来的控件无法用layout_alignParentBottom="true"之类的标签来调整。其实解决方法非常简单,只要你在include的时候同时重载下layout_width和layout_height这两个标签就可以了。如果不重载,任何针对include的layout调整都是无效的.这个最好亲自试验下就更能体会重载的重要性!
下面看看第二个例子

btn.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button">
</Button>
</LinearLayout> view.xml <!--
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
--> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000" /> </merge>

activity_main.xml

<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=".MainActivity" > <include android:id="@+id/layout1" layout="@layout/btn"/>
<include android:id="@+id/layout2" layout="@layout/btn"/>
<include layout="@layout/view"/> </LinearLayout>

可以看到在view.xml中用到了merge标签代替了FrameLayout,因为这里布局的根标签是FrameLayout,所以可以使用merge标签替换,如果根标签LinearLayout等就不能使用merge了.使用merge的好处是优化了UI结构,被导入的xml用merge作为根节点表示,这样当被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点,有兴趣的朋友可以通过hierarchyViewer工具查看下当前Ui资源的分配情况.

接着看java代码

package huahua.include2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout; public class MainActivity extends Activity { private LinearLayout layout1 = null;
private LinearLayout layout2 = null;
private Button btn1;
private Button btn2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); layout1 = (LinearLayout)findViewById(R.id.layout1);
layout2 = (LinearLayout)findViewById(R.id.layout2); btn1 = (Button)layout1.findViewById(R.id.btn);
btn1.setOnClickListener(new BtnClick());
btn2 = (Button)layout2.findViewById(R.id.btn);
btn2.setOnClickListener(new BtnClick());
} private class BtnClick implements View.OnClickListener{ @Override
public void onClick(View v) {
if(v.equals(btn1))
{
btn1.setText("点击是第一个按钮");
}
else if(v.equals(btn2))
{
btn2.setText("点击是第二个按钮");
} } } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

这时对于相同的R.id.btn要用不同的layout来区别就行了

代码:这里

Android include和merge标签的使用的更多相关文章

  1. 【转】在Android布局中使用include和merge标签

    内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...

  2. Android UI 优化 使用<include/>和 <merge />标签

    使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在Androi ...

  3. Android性能优化xml之<include>、<merge>、<ViewStub>标签的使用

    一.使用<include>标签对"重复代码"进行复用 <include>标签是我们进行Android开发中经常用到的标签,比如多个界面都同样用到了一个左侧筛 ...

  4. android 使用<merge>标签

    <merge /> 标签在你嵌套 Layout 时取消了 UI 层级中冗余的 ViewGroup .比如,如果你有一个 Layout 是一个竖直方向的 LinearLayout,其中包含两 ...

  5. Android布局优化之include、merge、ViewStub的使用

    本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味 ...

  6. Android 性能优化 四 布局优化merge标签的使用

    小白:之前分享了ViewStub标签的使用,Android还有其他优化布局的方式吗? 小黑:<merge />标签用于减少View树的层次来优化Android的布局.先来用个例子演示一下: ...

  7. Android的布局优化之include、merge 、viewstub

    以前在写布局的时候总是喜欢用自己熟悉的方式去写,从来也没有想过优化怎么的,后来又一次在上班的时候老大拿着我写的一个页面说我这个不行.我说这不是和设计图上的一模一样的么?怎么就不行了?然后他就跟我说了一 ...

  8. Android布局优化:include 、merge、ViewStub的详细总结

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要是对上篇博客的补充Android性能优化之UI渲染性能优化, 没有什么新东西,觉得应该是都掌握的玩意,写出来也只是自己做个小小的总结. ...

  9. Android 布局巧用之include、merge、ViewStub

    原文链接:https://mp.weixin.qq.com/s/bTA2gztUzqvqER2rz56RRQ 相信大家经常听到include.merge.ViewStub这样的标签,官方也提到这三种布 ...

随机推荐

  1. SQLite 入门教程(一)基本控制台(终端)命令

    一.基本简介 SQLite 是一个自持的(self-contained).无服务器的.零配置的.事务型的关系型数据库引擎.因为他很小,所以也可以作为嵌入式数据库内建在你的应用程序中.SQLite 被应 ...

  2. C# IO操作(三)文件编码

    在.net环境下新建一个文本文件(所谓文本文件就是直接可以用记事本打开的文件,直接保存字符串)和在系统中新建一个文本文件的编码是不一样的,.net默认采用UTF-8,而中文操作系统采用的是ANSI.如 ...

  3. asp.net 邮件发送类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. 【小丸类库系列】Word操作类

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...

  5. WIX Custom Action (immediate, deffered, rollback)

    Following content is directly reprinted from From MSI to WiX, Part 19 - The Art of Custom Action, Pa ...

  6. php判断手机浏览还是web浏览,并执行相应的动作

    正好需要,在网上找了好久,记录一下 function isMobile(){ $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTT ...

  7. composer php依赖管理工具

    #composer是什么 Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. composer出现之前我们php项目依赖管理大部分都是手动 ...

  8. R语言语法笔记

    ## 1. 数据输入 ## a$b # 数据框中的变量 a = 15 # 赋值 a <- 15 # 赋值 a = c(1,2,3,4,5) # 数组(向量) b = a[1] # 数组下标,从1 ...

  9. asp.net使用MVC4框架基于NPOI做导出数据到Excel表

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  10. php中的preg系列函数

    mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int ...