ViewSub 可以減少一開始配置Layout的memory

來達到初期加速的效果

使用時是單向的

被inflate後就不能再次對其充氣

適用於step by step的展示

 

1. 先到官網 http://developer.android.com/training/improving-layouts/loading-ondemand.html

01.png

 

2. 接著舉一個例子, 先創建一個要被inflate的layout, 這邊以resue.xml為例

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"/>

02.png

 

3. 接著在activity_main.xml使用ViewStub

這邊主要的是設置他的id, layout屬性

<ViewStub
android:id="@+id/stub_test"
android:inflatedId="@+id/stub_test_inflate"
android:layout="@layout/reuse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

03.png

 

4. 然後在MainActivity裡面, 可以用VISIBLE的屬性inflate

findViewById(R.id.stub_test).setVisibility(View.VISIBLE);

04.png

 

5. 或是用inflate()來使他顯現

((ViewStub)findViewById(R.id.stub_test)).inflate();

05.png

 

6. 可以看到結果如下

06.png

 

7. 而剛剛沒介紹到的inflatedId就是在inflate後可以控制的ID

 

07.png

 

8. 在顯現後可以再用INVISIBLE把其關閉

findViewById(R.id.stub_test_inflate).setVisibility(View.INVISIBLE);

08.png

 

9. 顯示結果如下, 最後如果畫面需要一值切換的

還是使用setVisibility來取代ViewSub

09.png  

 

arrow
arrow

    RX1226 發表在 痞客邦 留言(0) 人氣()