Android include和merge标签的使用
在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标签的使用的更多相关文章
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android UI 优化 使用<include/>和 <merge />标签
使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在Androi ...
- Android性能优化xml之<include>、<merge>、<ViewStub>标签的使用
一.使用<include>标签对"重复代码"进行复用 <include>标签是我们进行Android开发中经常用到的标签,比如多个界面都同样用到了一个左侧筛 ...
- android 使用<merge>标签
<merge /> 标签在你嵌套 Layout 时取消了 UI 层级中冗余的 ViewGroup .比如,如果你有一个 Layout 是一个竖直方向的 LinearLayout,其中包含两 ...
- Android布局优化之include、merge、ViewStub的使用
本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味 ...
- Android 性能优化 四 布局优化merge标签的使用
小白:之前分享了ViewStub标签的使用,Android还有其他优化布局的方式吗? 小黑:<merge />标签用于减少View树的层次来优化Android的布局.先来用个例子演示一下: ...
- Android的布局优化之include、merge 、viewstub
以前在写布局的时候总是喜欢用自己熟悉的方式去写,从来也没有想过优化怎么的,后来又一次在上班的时候老大拿着我写的一个页面说我这个不行.我说这不是和设计图上的一模一样的么?怎么就不行了?然后他就跟我说了一 ...
- Android布局优化:include 、merge、ViewStub的详细总结
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要是对上篇博客的补充Android性能优化之UI渲染性能优化, 没有什么新东西,觉得应该是都掌握的玩意,写出来也只是自己做个小小的总结. ...
- Android 布局巧用之include、merge、ViewStub
原文链接:https://mp.weixin.qq.com/s/bTA2gztUzqvqER2rz56RRQ 相信大家经常听到include.merge.ViewStub这样的标签,官方也提到这三种布 ...
随机推荐
- ASP.NET 上传图片添加文字、Logo水印
http://www.cnblogs.com/xvqm00/archive/2010/06/22/1762783.html
- 如何在ANDROID JNI 的C++中打Log
http://blog.csdn.net/pkigavin/article/details/8583537 最近在研究Android 2.3.3源代码的C/C++层,需要对代码进行一些调试,但是奇怪的 ...
- (int)、(int&)和(int*)的区别(转)
(1).首先通过一个例子看(int)和(int&)的区别: float a = 1.0f;cout << (int)a << endl;cout << (i ...
- 改善EF代码的方法(上)
本节,我们将介绍一些改善EF代码的相关方法,如NoTracking,GetObjectByKey, Include等. > MergeOption.NoTracking 当我们只需要读取某些数据 ...
- sql执行超时处理
首先设置数据库的配置文件 看看效果 如果程序还是超时则在连接字符串中做出处理 不然程序会自动kind的连接进程 程序和数据库方面都要配置缺一不可
- WIN7中oracle10g的安装注意事项
1.本次安装数据库版本为10.2.0.1,操作系统版本为windows7 32位 2.注意在"setup.exe"中以右键属性后,设置以兼容模式及以管理员身份运行该程序:在%安装文 ...
- jquery改变多个css样式
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax ...
- DataGridView 绑定数据后修改列类型
dataGridView1.DataSource = dt; dataGridView1.Columns.RemoveAt(); //删除要被替换的列 DataGridViewLinkColumn l ...
- C语言到底怎么了?
自2015年11月开始,所有C语言系列都呈现出衰落之势.在超过15年的时间里,C语言在编程语言排行榜中的占比一直有15%-20%,但今年却突然急转直下,目前占比已不足10%,且目前看来回天乏力. 那么 ...
- 折腾了一早上的C# WPF ListView+Grid 实现图片+文字 自动换行排列 类似Windows资源管理器效果
<ListBox Name="lstFileManager" Background ="Transparent" ItemsSource="{B ...