Android開發之merge結合include最佳化布局_Android

來源:互聯網
上載者:User

merge結合include最佳化android布局,效果不知道,個人感覺使用上也有很大的局限,不過還是瞭解一下,記錄下來。

布局檔案都要有根節點,但android中的布局嵌套過多會造成效能問題,於是在使用include嵌套的時候我們可以使用merge作為根節點,這樣可以減少布局嵌套,提高顯示速率。

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" > <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="張三" /> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="李四" /> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="王五" /></merge>

上面的介面在顯示的時候會自動嵌套到下面的檔案中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.viewstub.MainActivity" > <include layout="@layout/top"/> <TextView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/hello_world" /> <Button  android:id="@+id/toggle"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:onClick="onClick"  android:text="顯示/隱藏" /> <ViewStub  android:id="@+id/vs"  android:layout_margin="50dp"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:inflatedId="@+id/inflated_id"  android:layout="@layout/view_stub_layout" /></LinearLayout>

我為什麼說這個局限性比較大呢?因為merge中的空間顯示的使用會採用主布局檔案的方式來顯示,比如我這裡主布局是linearlayout且是水平排列,那麼merge中的元素顯示出來之後也是水平排列,可是我如果想讓merge中的元素垂直排列呢?抱歉,做不到。

原文連結:http://blog.csdn.net/u012702547/article/details/47133647

以上就是本文的全部內容,希望對大家學習Android軟體編程有所協助。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.