ViewStub的简单使用
以前只是单纯的写了个例子进行复习,今天又抽了点时间看了下,其实这个控件跟include作用都差不多,实现了view更模块化的管理,ViewStub 是一个隐藏的,不占用内存空间的视图对象,它可以在运行时延迟加载布局 资源文件.当 ViewStub 可见,或者调用 inflate()函数时,才会加载这个布局资源文件.简单点说,就是需要用到某一个view时,在适时的inflate出来出来进行使用.
今天就不改代码部分了,贴一个附件供大家方便的学习和了解http://www.agoit.com/images/smiles/icon_wink.gif
public class ViewStubEx extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.a);ViewStub mView = (ViewStub) findViewById(R.id.b_layout);mView.inflate();}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><ViewStub android:layout_width="fill_parent" android:layout="@layout/b"android:layout_height="100dp" android:id="@+id/b_layout" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="hello Kitty" /></LinearLayout>
详细的可以参考以下地址:
http://www.cnblogs.com/xirihanlin/archive/2010/04/28/1723291.html
页:
[1]