android stub 在哪定义,Android ViewStub的使用

简介

在Android开发中,布局的加载速度会影响APP的性能。如果布局实现的不好,会导致程序非常占内存并且UI运行缓慢。优化布局可以从三个方面着手:

使用标签重用layouts

使用标签避免冗余的布局嵌套

使用ViewStub实现按需加载

本文讲解一下ViewStub的使用。ViewStub相当于延迟加载,有的时候在布局中有一些不怎么重用的视图,可以只在需要的时候再加载,提高UI的渲染速度。

定义ViewStub

ViewStub是轻量级视图,不需要大小信息,不会在布局中绘制任何东西,每个 ViewStub 只需要设置android:layout属性来指定需要被 inflate 的 Layout 类型。

比如下面的布局文件:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="加载ViewStub" />

android:id="@+id/view_stub"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout="@layout/extra" />

布局中定义了一个ViewStub,layout属性引用了另一个布局extra.xml,这个布局就是被延迟加载的布局,而ViewStub本身不会显示任何内容。上面的Button是为了在代码中实现延迟加载。

extra.xml定义了一个TextView和一个progressBar,代码如下:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/text_view"

android:layout_width="match_parent"

android:layout_height="100dp"

android:background="#36e7ea"

android:gravity="center"

android:textSize="18sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center" />

加载ViewStub布局

要加载ViewStub引用的布局只需要调用inlfate()方法,在我们的这个例子中设置为点击Button,代码比较简单:

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

View view = ((ViewStub)findViewById(R.id.view_stub)).inflate();

TextView tv = (TextView) view.findViewById(R.id.text_view);

tv.setText("ViewStub的使用");

}

});

获取ViewStub之后直接调用inflate()即可把extra.xml解析为View,通过它可以得到extra.xml内部的控件,这样便实现了按需加载。

下面两幅图分别是点击Button前后的界面:

bVtUOS?w=346&h=619bVtUOZ?w=352&h=619

注意:ViewStub不支持使用标签的布局。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值