Android 布局巧用之include、merge、ViewStub

來源:互聯網
上載者:User

標籤:view   hub   idt   寬高   auto   載入   com   strong   開發   

原文連結:https://mp.weixin.qq.com/s/bTA2gztUzqvqER2rz56RRQ

相信大家經常聽到includemergeViewStub這樣的標籤,官方也提到這三種布局可用於布局的最佳化。今天就介紹下這三種布局的使用,記錄下來,便於後續app中的使用。

include布局重用

app開發過程中,會遇到不同頁面裡有相同的布局,這時我們可以將這些通用的布局提取出來到一個單獨的layout檔案裡,再使用<include>標籤引入到相應的頁面配置檔案裡,主要通過includelayout屬性引用。
舉個栗子
include的布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="這裡是來自include布局" /></RelativeLayout>

activity的布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="以下的內容來自include標籤" />    <include        android:id="@+id/container"        layout="@layout/include_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv"        android:layout_marginTop="10dp" /></RelativeLayout>

這個標籤在日常工作使用還是很常見的。這裡有幾點需要注意下:
1、如果給include標籤 和 include所載入的布局 都添加id的話,那麼id要保持一致,如例子中都是container,否則是在代碼中擷取不到RelativeLayout容器的。 當然我們可以避免這樣的問題,只需要給其中一項添加id屬性就可以。

2、include布局裡元素的id 要和 include所在頁面配置裡的其他元素id 不同,如例子中的兩個textview,如果把id設定相同了,程式運行起來並不會報錯,但是textview的賦值只會賦值給其中的一個。

3、如果需要給include標籤設定位置屬性的話,如例子中的layout_belowlayout_marginTop,這時候 必須 同時設定include標籤的寬高屬性layout_widthlayout_height,否則編譯器是會報錯的。一般情況不需要設定include的其他屬性,直接載入布局檔案 <include layout="@layout/...."/>

4、布局中可以包含兩個相同的include標籤,如下代碼所示 兩個include都載入layout="@layout/include_layout"

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="以下的內容來自include標籤" />    <include        android:id="@+id/container"        layout="@layout/include_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv"        android:layout_marginTop="10dp" />    <include        android:id="@+id/container2"        layout="@layout/include_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="80dp" /></RelativeLayout>

可以設定不同include的id屬性,引用的時候如下可以正常顯示:

View view = findViewById(R.id.container2);TextView textView = view.findViewById(R.id.tv);textView.setText("這裡是來自 第二個 include布局");
merge減少視圖層級

merge標籤可用於減少視圖層級來最佳化布局,可以配合include使用,如果include標籤的父布局 和 include布局的根容器是相同類型的,那麼根容器的可以使用merge代替。
頁面配置

<?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">    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="以下的內容不是來自merge標籤" />    <include        layout="@layout/merge_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp" /></LinearLayout>

先看沒有使用merge的:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="這裡是不是來自merge布局" /></LinearLayout>

看下view層的結構:

再看使用了merge的:

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android">    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="這裡是來自merge布局" /></merge>

view層結構:

可以看到對比,減少了一層的LinearLayout的嵌套,需要注意的是使用merge的布局,在include的標籤設定距離屬性沒有生效,可以將一些間距屬性設定到include布局裡元素上,具體看項目需求使用。

ViewStub按需載入

按需載入 顧名思義需要的時候再去載入,不需要的時候可以不用載入,節約記憶體使用量。通常情況我們會使用setVisibility方法來控制視圖的顯示和隱藏,但是這種情況視圖已經載入了。
比如app中頁面裡某個布局只需要在特定的情況下才顯示,其餘情況下可以不用載入顯示,這時候可以使用ViewStub
layout屬性是需要載入布局

<?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">    <ViewStub        android:id="@+id/viewstub"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:layout="@layout/viewstub_layout" /></LinearLayout>

需要注意的是 ViewStubinflate()方法只能被調用一次,一旦調用後,ViewStub將從視圖中移除,被對應的layout布局取代,同時會保留ViewStub上設定的屬性效果。

ViewStub viewstub = findViewById(R.id.viewstub);viewstub.inflate();

這篇關於includemergeViewStub的使用就介紹到這裡了,具體使用方式還得視項目而定。

最後附上github地址https://github.com/taixiang/include

歡迎關注我的部落格:https://blog.manjiexiang.cn/
更多精彩歡迎關注號:春風十裡不如認識你

Android 布局巧用之include、merge、ViewStub

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.