<LinearLayout
android:orientation="vertical"android:layout_height="match_parent"android:layout_width="match_parent"android:id="@+id/container"xmlns:tools="http://schemas.android.com/tools"xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btn" android:text="下面试include引用"/>
<include layout="@layout/include"/>
<include layout="@layout/include"/>
<include layout="@layout/include"/>
<include layout="@layout/include"/>
</LinearLayout>
<LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><includeandroid:id="@+id/include4"layout="@layout/include"/></LinearLayout>
android:layout_width="wrap_content"android:layout_height="wrap_content"
include.xml 里面是一个文本
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextViewandroid:id="@+id/aaa"android:layout_height="wrap_content"android:layout_width="fill_parent"android:textSize="24sp"android:text="这是include里面的aaa TextView"android:background="#00ff00"/>
package com.example.includetest;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view=LayoutInflater.from(this).inflate(R.layout.include, null);
Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "从 include 引用 main中的Button", Toast.LENGTH_LONG).show();
}
});
}
在RelativeLayout中使用include标签需注意
经常会有同学在RelativeLayout中使用include标签
但是却发现include进来的控件无法用layout_alignParentBottom="true"之类的标签来调整。这个真的非常恼火。其实解决方法非常简单,只要你在include的时候同时重载下layout_width和layout_height这两个标签就可以了。如果不重载,任何针对include的layout调整都是无效的!
关于Merge标签
传送门:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/
在android布局中使用include和merge标签
.使用<merge/>标签
merge标签用来消除我们在include一个布局到另一个布局时所产生的冗余view group。比如现在很多布局中会有两个连续的Button,于是我们将这两个连续的Button做成可复用布局(re-usable layout)。在使用include标签时我们必须先将这两个Button用一个view group比如LinearLayout组织在一起然后供其它布局使用,如果是include的地方也是LiearLayout就会造成有两层连续的LiearLayout,除了降低UI性能没有任何好处。这个时候我们就可以使用<merge/>标签作为可复用布局的root view来避免这个问题。
- <merge xmlns:android="http://schemas.android.com/apk/res/android">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/add"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/delete"/>
- </merge>
当我们用<include/>标签复用上述代码时,系统会忽略merge元素,直接将两个连续的Button放在<include/>标签所在处。

本文详细探讨了Android布局中Include与Merge标签的应用技巧。通过实战案例解析如何避免重复控件导致的问题,确保Include标签能够正确加载多次而不重复,并介绍了Merge标签在减少冗余ViewGroup中的作用。
315

被折叠的 条评论
为什么被折叠?



