Android基础知识AndroidUIAndroid

ViewStub 用法详解

2016-09-09  本文已影响18905人  蚊子Skeeter

介绍

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

优点

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

缺点

用法

<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();

注意

实例

<!-- 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
上一篇 下一篇

猜你喜欢

热点阅读