1.ViewStub继承关系
public final class ViewStub
extends View
java.lang.Object
↳ android.view.View
↳ android.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()方法的实践。