使用惰性控件ViewStub实现布局动态加载

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值