在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. PHP实现http与https转化

    http://zyan.cc/post/142/ 最近在写PHP程序时,需要使浏览器在https和http之间转化,上网搜索相关信息,无奈只有最近在写PHP程序时,需要使浏览器在https和http之 ...

  2. 使用 x3dom 框架及 WebGL 在浏览器上显示 3 维模型

    如果需要在浏览器上显示 3D 画面的话, 现在一般会使用 ​WebGL, 典型的例如 three.js(​http://mrdoob.github.com/three.js/), 但是 WebGL 对 ...

  3. C# 高精度减法 支持小数(待优化)

    是现实思路 1,先小数点补位,8913758923475893274958738945793845-4893127498372459823745324532453245.284929384729837 ...

  4. Vijos P1325桐桐的糖果计划

    > P1325桐桐的糖果计划 标签:**图结构 强连通分量** 描述 桐桐很喜欢吃棒棒糖.他家处在一大堆糖果店的附近. 但是,他们家的区域经常出现塞车.塞人等情况,这导致他不得不等到塞的车或人走 ...

  5. poj代码搬家啦啦啦

    我的poj代码搬家啦,大家想看可以到 blog.csdn.net/michaelysm 来看.欢迎哦

  6. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  7. Eclipse代码自动提示

    发现了一个好用的快捷键,不用跳转到源文件就可以看源代码:先按shift键,然后鼠标点击要看的代码,即可.

  8. jQuery 扩展 【ajax实例】

    先前写工具类都是自定义类,直接prototype,jQuery扩展这一块儿,一直也没写过,刚好今天有空,写个试试. 已经有很多人对jQuery,jQuery.fn,jQuery.fn.extend详细 ...

  9. ASP.NET DataList嵌套实现评论效果

    问题: Datalist1显示say这个表的数据 然后想在Datalist1中嵌套一个Datalist2用于显示对应的评论表2的 sayID对应表1的id,若表2中找不到对应sayId则在对应的Dat ...

  10. Winform窗口弹出位置控制

    窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: WindowsDefaultBound ...