Android佈局優化----ViewStub、include、merge

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爲null
TextView 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");


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章