【Android 开发】:UI控件之 ViewStub 惰性装载控件的使用

1. ViewtSub 的概要 

    之前我们介绍过<include>标签,该标签可以再布局文件中引用另外一个布局文件,这种方式是在布局文件中固定导入,使用起来不是很方便。

    ViewtSub的功能和<include>的功能类似,也是实现引用另外一个布局。但是唯一不同的是ViewStub并不会马上装载引用布局文件,只有在调用了ViewStub.inflate或ViewStub.setVisibility(View.VISIBILE)方法ViewStub才会装载引用的控件。

<include /> 这个标签表示加载一个静态的布局文件

2. 查看一下 ViewStub api文档。ViewStub是一个final类,它是一个可见的,零大小的视图,它可以在运行的时候惰性填充布局资源,当一个惰性资源可见的时候,当调用inflate()方法后,指定这个布局资源就会被加载。这个加载的布局就会替换掉原有的布局了。


3. 案例实现

1. 布局文件

<?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"
    android:orientation="vertical" >

    <!-- 静态的加载布局文件,当UI启动的时候,它会讲next.xml的布局加载进去 -->

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/next" />

    <!-- 动态的加载布局文件 -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFCCDD" >

        <ViewStub
            android:id="@+id/stub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/next" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态添加布局" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动态隐藏布局" />

</LinearLayout>
2. 需要动态加载的布局 next.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
3. 程序主文件

public class ViewStubDemo extends Activity {
    
    private Button button1, button2;
    private ViewStub viewStub;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initComponent();
        button1.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                //表示填充动态布局
                View view = viewStub.inflate();            
                LinearLayout layout = (LinearLayout)view;
                RatingBar bar = (RatingBar)layout.findViewById(R.id.ratingBar1);
                bar.setNumStars(3); //设置星星为3科
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewStub.setVisibility(View.GONE); //隐藏动态加载的布局
                

                
            }
        });
    }
    
    private void initComponent(){
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        viewStub = (ViewStub)findViewById(R.id.stub);
    }
}

4. 案例执行结果

这个案例在项目可以运用到这种地方:在客户端上指定一个区域用来显示从服务端下载下来的数据,当用户想要显示的时候可以就可以在这个区域上动态显示这些内容了。而静态布局是UI加载过程就已经固定的摆放在指定区域了。

4. 程序说明

1): 利用ViewStub显示和隐藏布局 ViewStub的引入: 在开发的时候,有些布局是要根据条件而动态显示,达到一个布局两用的效果,运用View.VISIBLE和View.GONE去改变布局的可见性. 这样的做法显然是没什么多大的问题,优点逻辑清晰,控制灵活,但缺点就是耗费资源

2): 在setContentView()或者用inflate加载布局文件时无论View是否被设置为View.GONE和View.VISIBLE,都会创建对象,占用一定程度上的内存,所以在考虑优化程序的时候,尽量避免资源浪费,降低程序的资源占有量,提高响应速度,提升软件的用户体验,推荐的做法是使用android.view.ViewStub. ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件.

3): ViewStub是一个隐藏的,不占用内存空间的视图对象,它可以在运行时延迟加载布局资源文件当 ViewStub可见,或者调用inflate()函数时,才会加载这个布局资源文件 注意的问题: ViewStub只能用来Inflate一个布局文件,而不是某个具体的View

4): [程序异常] 报错 ViewStub must have a non-null ViewGroup viewParent 原因: 官方文档:viewstub不能反复inflate,只能inflate一次


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值