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

来源:互联网 发布:java 图片尺寸 编辑:程序博客网 时间:2024/05/29 13:49

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       <ViewStub15           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               <Button28                   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               <Button34                   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>


0 0
原创粉丝点击