日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

include、ViewStub、merge优化布局标签

發布時間:2025/4/5 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 include、ViewStub、merge优化布局标签 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

在寫Android的xml布局時,用好 include、ViewStub、merge這三個標簽,可以是我們的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"><Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"/><TextViewandroid: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"><includeandroid: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 中延時加載的布局有兩種方式:

  • 在xml中使用 android:layout 屬性來設置。
  • 在代碼中使用 ViewStub.setLayoutResource(res); 來設置。
  • 使ViewStub中布局顯示出來也有兩種方法:

  • 調用 ViewStub.setVisibility(View.VISIBLE);。
  • 調用 ViewStub.inflate();。
  • 這兩個方法本質上都是調用ViewStub.inflate();來實現布局的加載顯示。

    舉例說明:
    <ViewStubandroid: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"> <Buttonandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"/><TextViewandroid:id="@+id/textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView"/> </merge>

    作者:Pan_大寶
    鏈接:http://www.jianshu.com/p/354fb8a42ad8
    來源:簡書
    著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

    總結

    以上是生活随笔為你收集整理的include、ViewStub、merge优化布局标签的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。