Android布局优化(一) ViewStub详解

1.ViewStub继承关系

public final class ViewStub 
extends View 
java.lang.Objectandroid.view.Viewandroid.view.ViewStub

官网对ViewStub的定义:A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime。
意思是ViewStub是一个不可见零尺寸被用来在运行时延迟加载布局资源的视图。

2.ViewStub懒加载原理:

<ViewStub 
       android:id="@+id/stub"
       android:inflatedId="@+id/subTree"
       android:layout="@layout/mySubTree"
       android:layout_width="120dip"
       android:layout_height="40dip" />

当调用ViewStub的成员方法setVisibility(int) or inflate()时,ViewStub本身被mySubTree对应的布局替换,ViewStub中的布局参数也会替换mySubTree根布局中的布局参数,比如 inflatedId会替换myStubTree根布局的id,layout_with,layout_height会替换掉mySubTree布局中设置的宽高,即上面的120dip,40dip会被设置为mySubTree根布局的宽高,mySubTree根布局原来设置的宽高无效。
3.ViewStub的使用

方式一:
ViewStub stub = findViewById(R.id.stub);
View inflated = stub.inflate();

方式二:
ViewStub stub = findViewById(R.id.stub);
stub.setVisiblity(View.Visible);

例子:
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="showStub" />

    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/layout_include" />
 </LinearLayout>

layout_include.xml

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

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ViewStub" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello" />

</LinearLayout>

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {
    private final String TAG = getClass().getSimpleName();
    private LinearLayout ll;
    private Button btn1, btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        findViewById(R.id.btn_stub).setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        //方式一
//        ViewStub viewStub = findViewById(R.id.view_stub);
//        if (ll == null) {
//            // inflate()方法返回的为要加载的布局
//            ll = (LinearLayout) viewStub.inflate();
//            btn1 = findViewById(R.id.btn1);
//            btn2 = findViewById(R.id.btn2);
//        } else {
//            //布局已经加载
//        }
//        btn1.setText("hello");
//        btn2.setText("world");
//    方式二
        ViewStub viewStub = findViewById(R.id.view_stub);
        if (ll == null) {
            viewStub.setVisibility(View.VISIBLE);
            ll = findViewById(R.id.linear1);
        } else {
            Toast.makeText(this, "布局已加载", Toast.LENGTH_SHORT).show();
        }


    }
}

上面两种方式分别是对inflate(),和setVisibility()方法的实践。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值