Android布局最佳化----ViewStub、include、merge

來源:互聯網
上載者:User

標籤:android   標籤   include   

1 StubView

作用:StubView標籤中的布局只有在需要的時候才會被渲染載入。

注意:StubView的渲染載入操作只能執行一次;不支援merge標籤

使用樣本:

(1)ViewStub中引用的布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:id="@+id/stublayout"    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/sbtv"        android:layout_width="300dp"        android:layout_height="300dp"        android:text="this is sub text view"/></LinearLayout>

(2)使用ViewStub

<ViewStub    android:id="@+id/viewstub"    android:layout_width="100dp"    android:layout_height="100dp"    android:layout="@layout/sublayout"/>

(3)java代碼中渲染載入

ViewStub stub = (ViewStub)findViewById(R.id.viewstub);stub.inflate();TextView stubtv = (TextView)layout.findViewById(R.id.sbtv);stubtv.setText("hello stub!");


2 include標籤

作用:將引用的布局替換到當前布局中該標籤所處的位置;

注意:引用的布局非merge,設定inlude的id屬性後會覆蓋掉引用布局頂層layout的id;

樣本1 引用布局非merge

(1)引用布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/sharedlayout2"    android:layout_width="200dp"    android:layout_height="200dp"    android:background="@android:color/black">    <TextView        android:id="@+id/mytv"        android:layout_width="100dp"        android:layout_height="100dp"        android:background="@android:color/holo_blue_light"        android:text="這時一個公用的布局"/></LinearLayout>

(2)使用include標籤

<include    android:id="@+id/include1"<!--該id會覆蓋布局(1)中LinearLayout的id;-->    layout="@layout/my"    android:layout_width="50dp"    android:layout_height="50dp"></include>

(3)java中使用

LinearLayout sharedlayout = (LinearLayout) findViewById(R.id.include1);TextView tv = (TextView) sharedlayout.findViewById(R.id.mytv);tv.setText("hello include1");

樣本2 引用merge布局

(1)引用布局

<merge xmlns:android="http://schemas.android.com/apk/res/android">    <LinearLayout        android:id="@+id/mergetlayout"        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:id="@+id/mergetv1"            android:layout_width="40dp"            android:layout_height="40dp"            android:background="@android:color/holo_blue_bright"            android:text="merge text1" />        <TextView            android:id="@+id/mergetv2"            android:layout_width="40dp"            android:layout_height="40dp"            android:background="@android:color/darker_gray"            android:text="merge text2" />    </LinearLayout></merge>

(2)include標籤:merge布局沒有id屬性,所以這裡的id其實沒有意義

<include    android:id="@+id/include2"    layout="@layout/mymerge"></include>

(3)java中使用

//LinearLayout layout = (LinearLayout)findViewById(R.id.include2);//通過該代碼無法擷取(1)                                                    中的LinearLayout,這裡的layout為nullTextView mTV1 = (TextView)findViewById(R.id.mergetv1);mTV1.setText("hello merge tv1");TextView mtv2 = (TextView)findViewById(R.id.mergetv2);mtv2.setText("hello merge tv2");

3 merge標籤

    merge標籤相當於控制項的簡單集合,被include到其他布局中後根據所處容器布局結合各個控制項的相關屬性進行布局。

注意:merge標籤沒有id屬性

使用樣本:

(1)merge布局

<merge xmlns:android="http://schemas.android.com/apk/res/android">    <TextView        android:id="@+id/mergetv1"        android:layout_width="40dp"        android:layout_height="40dp"        android:background="@android:color/holo_blue_bright"        android:text="merge text1" />    <TextView        android:id="@+id/mergetv2"        android:layout_width="40dp"        android:layout_height="40dp"        android:background="@android:color/darker_gray"        android:text="merge text2" /></merge>

此時的布局為兩個textview重疊並且mergetv2處於上層;

(2)merge標籤使用

<include    android:id="@+id/include2"    layout="@layout/mymerge"></include>

(3)由於該include標籤處於一個豎直的linearlayout中,此時merge布局的顯示效果如下:

merge text 1
merge text 2
(4)java中使用
TextView mTV1 = (TextView)findViewById(R.id.mergetv1);mTV1.setText("hello merge tv1");TextView mtv2 = (TextView)findViewById(R.id.mergetv2);mtv2.setText("hello merge tv2");


本文出自 “wauoen” 部落格,請務必保留此出處http://7183397.blog.51cto.com/7173397/1847175

Android布局最佳化----ViewStub、include、merge

聯繫我們

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