android 布局优化——<include>的是使用

来源:互联网 发布:淘宝卖的黄金是真的吗 编辑:程序博客网 时间:2024/06/10 03:05

<include>标签可以允许在一个布局当中引入另外一个布局,那么比如说我们程序的所有界面都有一个公共的部分,这个时候最好的做法就是将这个公共的部分提取到一个独立的布局文件当中,然后在每个界面的布局文件当中来引用这个公共的布局。

引用 方式:  <include layout="@layout/titlebar" />   其中titlebar代表是公共的布局

<merge>


<merge>标签是作为<include>标签的一种辅助扩展来使用的,它的主要作用是为了防止在引用布局文件时产生多余的布局嵌套。大家都知道,Android去解析和展示一个布局是需要消耗时间的,布局嵌套的越多,那么解析起来就越耗时,性能也就越差,因此我们在编写布局文件时应该让嵌套的层数越少越好。


使用方法:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <Button  
  5.         android:id="@+id/ok"  
  6.         android:layout_width="match_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:layout_marginLeft="20dp"  
  9.         android:layout_marginRight="20dp"  
  10.         android:text="OK" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/cancel"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_marginLeft="20dp"  
  17.         android:layout_marginRight="20dp"  
  18.         android:layout_marginTop="10dp"  
  19.         android:text="Cancel" />  
  20.   
  21. </merge>  

阅读全文
0 0
原创粉丝点击