ViewStub:是一个看不见,0大小的视图,不会占用内存,且动态加载布局,提高代码性能。
场景:xml布局有很多view,各种状态,如果这个时候把一切状态下的视图也一并写入就会使代码复杂,难懂,这个时候ViewStub就出现了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:orientation="vertical"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent" >
6
7 <!-- 静态加载 -->
8 <include
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 layout="@layout/includelayout" >
12 </include>
13
14 <ViewStub
15 android:id="@+id/viewstub"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_marginLeft="2dp"
19 android:layout="@+layout/viewstublayout" >
20 </ViewStub>
21
22 <!-- 按钮 -->
23 <LinearLayout
24 android:orientation="horizontal"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content" >
27 <Button
28 android:id="@+id/button_extend"
29 android:layout_weight="1"
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:text="展开宝贝详细描述" />
33 <Button
34 android:id="@+id/button_hide"
35 android:layout_weight="1"
36 android:layout_width="wrap_content"
37 android:layout_height="wrap_content"
38 android:text="隐藏宝贝详细描述" />
39 </LinearLayout>
40
41 </LinearLayout>