ViewStub 用法详解

介绍

ViewStub 是一个轻量级的View,没有尺寸,不绘制任何东西,因此绘制或者移除时更省时。(ViewStub不可见,大小为0)

优点

实现View的延迟加载,避免资源的浪费,减少渲染时间,在需要的时候才加载View

缺点

  • ViewStub所要替代的layout文件中不能有<merge>标签
  • ViewStub在加载完后会被移除,或者说是被加载进来的layout替换掉了

用法

<ViewStub
    android:id="@+id/stub_import"
    android:inflatedId="@+id/panel_import"
    android:layout="@layout/progress_overlay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

用ViewStub加载layout文件时,可以调用 setVisibility(View.VISIBLE) 或者 inflate()

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

注意

  • 一旦ViewStub visible/inflated,则ViewStub将从视图框架中移除,其id stub_import 也会失效
  • ViewStub被绘制完成的layout文件取代,并且该layout文件的root view的id是android:inflatedId指定的id panel_import,root view的布局和ViewStub视图的布局保持一致

实例

<!-- layout_viewstub.xml 要延迟加载的view -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/layout_viewstub_old"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@android:color/darker_gray"
        android:padding="5dp"
        android:text="This is the layout instead of ViewStub view."/>

</LinearLayout>
<!-- act_test_viewstub.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:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/act_test_viewstub_tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@android:color/darker_gray"
        android:padding="5dp"
        android:text="Show ViewStub"/>

    <ViewStub
        android:id="@+id/act_test_viewstub_viewstub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/act_layout_viewstub_new"
        android:layout="@layout/layout_viewstub"/>

    <!--<include-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--layout="@layout/layout_viewstub"/>-->

</LinearLayout>
public class ViewStubTestActivity extends FragmentActivity {
    private static final String TAG = "test_viewstub";
    protected ViewStub mViewStub;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_test_viewstub);
        mViewStub = (ViewStub) findViewById(R.id.act_test_viewstub_viewstub);

        Log.e(TAG, "viewstub: " + findViewById(R.id.act_test_viewstub_viewstub));
        Log.e(TAG, "layout: " + findViewById(R.id.act_layout_viewstub_new));

        findViewById(R.id.act_test_viewstub_tv_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View layoutView;
//                mViewStub.setVisibility(View.VISIBLE);
                layoutView = mViewStub.inflate();

                Log.e(TAG, "mViewStub: " + mViewStub);
                // ViewStub在visible/inflated后会被移除,所以此处为null
                Log.e(TAG, "viewstub: " + findViewById(R.id.act_test_viewstub_viewstub));
//                layoutView = findViewById(R.id.act_layout_viewstub_new);
                Log.e(TAG, "layoutView equals finviewbyid(layout): " +
                        layoutView.equals(findViewById(R.id.act_layout_viewstub_new)));
                Log.e(TAG, "layout: " + layoutView);

                if (layoutView != null) {
                    // layoutView的root view id 是mViewStub inflatedId指定的ID
                    if (layoutView.getId() == R.id.act_layout_viewstub_new) {
                        Log.e(TAG, "layout root id is act_layout_viewstub_new");
                    } else if (layoutView.getId() == R.id.layout_viewstub_old) {
                        Log.e(TAG, "layout root id is layout_viewstub_old");
                    } else {
                        Log.e(TAG, "layout root id is anyone : " + layoutView.getId());
                    }

                    // layoutView的root view布局 和mViewStub的布局保持一致
                    int width = layoutView.getLayoutParams().width;
                    if (width == ViewGroup.LayoutParams.MATCH_PARENT) {
                        Log.e(TAG, "layout width is MATCH_PARENT");
                    } else if (width == ViewGroup.LayoutParams.WRAP_CONTENT) {
                        Log.e(TAG, "layout width is WRAP_CONTENT");
                    } else {
                        Log.e(TAG, "layout width is anyone : " + width);
                    }
                }
            }
        });

    }
}
运行结果.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,632评论 0 17
  • 太长不看版:在 Android UI 布局过程中,遵守一些惯用、有效的布局原则,可以制作出高效且复用性高的 UI。...
    Mupceet阅读 3,930评论 0 14
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,885评论 25 709
  • 长篇小说如果在网络上发布,无字数限定,可以写成无底洞。如果要选择出版,直接关系到出版社的投资与回报,字数多少、印数...
    陈铱阅读 417评论 0 2
  • 十年前,TED当道的时候,第一次听说这位老兄的时候,是因为他和他的牛X气泡图。 为了配合讲解这个带有时间线的动态可...
    _wildplant_阅读 2,504评论 0 2