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

内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/

在我们开发android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写。

1. 创建一个可以重复使用的布局:

如下代码描述在应用中每个acitivity都出现的顶栏titlebar.xml

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width=”match_parent”
 3     android:layout_height="wrap_content"
 4     android:background="@color/titlebar_bg">
 5 
 6     <ImageView android:id="@+id/title"
 7                android:layout_width="wrap_content"
 8                android:layout_height="wrap_content" 
 9                android:src="@drawable/gafricalogo" />
10 </FrameLayout>

上面的根布局(root view)即frameLayout会出现在之后插入的地方。

2. 使用<include/>标签:

在应用中的一个activity的布局中顶栏就是如上的布局,那么我们就可以include上面的titlebar.xml达到复用的效果,布局代码如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical" 
 3     android:layout_width=”match_parent”
 4     android:layout_height=”match_parent”
 5     android:background="@color/app_bg"
 6     android:gravity="center_horizontal">
 7 
 8     <include  android:id="@+id/new_title"
 9               layout="@layout/titlebar"/>
10 
11     <TextView android:layout_width=”match_parent”
12               android:layout_height="wrap_content"
13               android:text="@string/hello"
14               android:padding="10dp" />
15 
16     ...
17 
18 </LinearLayout>

通过取得元素的id,我们可以修改include标签中元素的属性,下面的例子为在actiivty中修改titlebar中的图片:

1 private View mTitleBar = null;
2 private ImageView mTitleImageView = null;
3 
4 mTitleBar = findViewById(R.id.new_title);
5 mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title);
6 mTitleImageView.setImageResource(R.drawable.logo);

在使用include标签时,我们可以覆写插入布局root view的属性(所有的android:layout_*属性):

1 <include android:id=”@+id/news_title”
2          android:layout_width=”match_parent”
3          android:layout_height=”match_parent”
4          layout=”@layout/title”/>

如果需要覆写插入布局root view的属性,则必须制定android:layout_width和android:layout_height这两个属性以使其它的覆写属性生效。

注意:被引用的布局的属性中,只有,外层的layout属性可以被修改,内部属性不能被修改。

举例:button2.xml,内容如下:

1 <Button xmlns:android="http://schemas.android.com/apk/res/android"
2     android:id="@+id/button1"
3     android:layout_width="wrap_content"
4     android:layout_height="wrap_content"
5     android:text="Button" />
那么在别的地方引用这个布局:
 
        <include
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button23456"
            layout="@layout/button2" />
 

 那么,绿色的代码设置的属性是有效的,红色的代码设置的属性是无效的。

其实,这很容易理解,就是include是为了复用一段封装好的布局,那么,布局内部的东西自然是不用改的,如果要改,还不如在被引用的文件中改好,我们在include中修改的只是被引用的布局的大小位置。

3. 使用<merge/>标签

merge标签用来消除我们在include一个布局到另一个布局时所产生的冗余view group。比如现在很多布局中会有两个连续的Button,于是我们将这两个连续的Button做成可复用布局(re-usable layout)。在使用include标签时我们必须先将这两个Button用一个view group比如LinearLayout组织在一起然后供其它布局使用,如果是include的地方也是LiearLayout就会造成有两层连续的LiearLayout,除了降低UI性能没有任何好处。这个时候我们就可以使用<merge/>标签作为可复用布局的root view来避免这个问题。

 1 <merge xmlns:android="http://schemas.android.com/apk/res/android">
 2 
 3     <Button
 4         android:layout_width="fill_parent" 
 5         android:layout_height="wrap_content"
 6         android:text="@string/add"/>
 7 
 8     <Button
 9         android:layout_width="fill_parent" 
10         android:layout_height="wrap_content"
11         android:text="@string/delete"/>
12 
13 </merge>

当我们用<include/>标签复用上述代码时,系统会忽略merge元素,直接将两个连续的Button放在<include/>标签所在处。

————————————————————————————————————————————————
来自企鹅娘的问候:
欢迎交流哦,如果有帮助转载的话,请务必注明出处"企鹅娘's 学习笔记",让我也小小的开心一下
原文地址:https://www.cnblogs.com/hopecapital/p/4673570.html