android 布局 include和merge

来源:互联网 发布:好一点的cos淘宝店 编辑:程序博客网 时间:2024/05/04 17:07

android布局文件可以通过使用include和merge标签提高绘制效率。
include标签用于引入通用的布局代码。
merge标签则用于消除多余的层级。
给一个例子:

activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/back"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >    <include        android:id="@+id/include"        layout="@layout/layout_item" /></LinearLayout>
layout_item.xml<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android">    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        /></merge>

注意
1,include标签用于重新放置已有的布局,因此只能修改layout属性和id。android:weight属性也不能使用
2,如果需要复用的布局含有多个item,必须有一个包含的父布局,如果不用merge就会多产生一层,造成性能开销。所以多个item组件需要用merge标签来消除include时多产生的一层。

0 0