Android布局优化之include、ViewStub、merge

前言

在写Android的xml布局时,用好 includeViewStubmerge这三个标签,可以是我们的xml更加简洁、高效。

include

按照官方的意思,include就是为了解决重复定义相同布局的问题。
相当于Java代码中将相同的部分抽取出来,然后复用,需要的时候引入它即可,而不必每次都自己写一遍。

举例说明:

一个公共布局文件 my_layout.xml(这个布局后面例子也会用到):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/linearLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"/>
</LinearLayout>

使用这个公共布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <include
        android:id="@+id/include_layout"
        layout="@layout/my_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
注意事项:

使用 include 最常见的问题就是 findViewById 时候出现 NullPointerException
这个问题出现的前提是在 <include /> 中设置了id,被 include 进来的布局的根元素也设置了id。
那么这时使用被 include 进来的根元素id进行 findViewById 就会出现NullPointerException

在上述例子中:

findViewById(R.id.include_layout);// 正常
findViewById(R.id.linearLayout);// 会出现NullPointerException
findViewById(R.id.button);// 正常
findViewById(R.id.textView); // 正常

ViewStub

ViewStub就是一个宽高都为0的一个View,它默认是不可见的。
只有通过调用 setVisibility() 函数或者 Inflate() 函数才会将其要装载的目标布局给加载出来,从而达到延迟加载的效果。
ViewStub布局可显示之前,系统不会消耗资源去实例化里面的布局,可以节省系统资源消耗。

设置 ViewStub 中延时加载的布局有两种方式:

  1. 在xml中使用 android:layout 属性来设置。
  2. 在代码中使用 ViewStub.setLayoutResource(res); 来设置。

使ViewStub中布局显示出来也有两种方法:

  1. 调用 ViewStub.setVisibility(View.VISIBLE);
  2. 调用 ViewStub.inflate();

这两个方法本质上都是调用ViewStub.inflate();来实现布局的加载显示。

举例说明:
<ViewStub
    android:id="@+id/view_stub"
    android:layout_width="fill_parent"
    android:layout_height="49dp"
    android:layout="@layout/my_layout"
    android:inflatedId="@+id/view_stub_inflated_id"/>
// my_layout 布局文件就是上文中的 my_layout.xml 
// 第一种使用方法:
//使用android:layout="@layout/my_layout"设置布局
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);  
//设置setVisibility,使布局文件实例化
viewStub .setVisibility(View.VISIBLE);  
// 通过ViewStub的xml中的属性 inflatedId 来获取View
LinearLayout linearLayout =(LinearLayout) findViewById(R.id.view_stub_inflated_id);  
if ( viewStub.getVisibility() == View.VISIBLE ) {  
    // 已经加载成功
}
// 第二种使用方法:
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);  
viewStub.setLayoutResource(R.layout.my_layout);
//使用 inflate,使布局文件实例化
LinearLayout linearLayout= (LinearLayout)viewStub.inflate();
if (linearLayout!= null){ 
    //已经加载成功
} 
 

merge

merge它可以删减多余的层级,优化UI。
例如你的主布局文件是垂直的LinearLayout,这时使用includemy_layout.xml 引入进来。
新布局也是垂直的LinearLayout,那么这个新的LinearLayout就没有任何意义了。使用的话反而增加反应时间。这时可以使用<merge/>标签优化。

merge 原理就是在解析xml时候,如果是 <merge/> 标签,那么直接将其中的子元素添加到merge 标签parent中,这样就保证了不会引入额外的层级

示例如下 :

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"/>
</merge>
    原文作者:Pan_大宝
    原文地址: https://www.jianshu.com/p/354fb8a42ad8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞