android

来源:互联网 发布:淘宝1.74非球面镜片 编辑:程序博客网 时间:2024/06/16 05:44

ViewStub是Android中view的一种优化方案,它的目的是在不需要显示view的时候不去加载view,这样在view的创建时期,减少了加载的资源,优化了view。

invisible 、gone 、 viewstub的区别:

1、invisible
view设置为invisible时,view在layout布局文件中会占用位置,但是view为不可见,该view还是会创建对象,会被初始化,会占用资源。

2、gone
view设置gone时,view在layout布局文件中不占用位置,但是该view还是会创建对象,会被初始化,会占用资源

3、viewstub
viewstub是一个轻量级的view,它不可见,不用占用资源,只有设置viewstub为visible或者调用其inflater()方法时,其对应的布局文件才会被初始化。但是viewstub的引用对象需要是一个布局layout文件,如果要是单个的view的话,viewstub就不能满足要求,就需要调用view的gone或者invisible属性了。

activity_mainiii.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:orientation="vertical">    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Traggle" />    <ViewStub        android:id="@+id/stub"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout="@layout/text_layout" />    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Traggle2" /></LinearLayout>

text_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="ddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddddd \n    ddddddddddddddddddddddddddddddddddddd"></TextView>

ViewStubActivity.java

package com.example.administrator.myapplication;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewStub;import android.widget.Button;/** * Created by Administrator on 2017/6/1. */public class ViewStubActivity extends AppCompatActivity {    private boolean mShowed;    private View mTextPanel;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_mainiii);        Button btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (mTextPanel == null) {                    mTextPanel = ((ViewStub) findViewById(R.id.stub)).inflate();                }                if (mShowed == false) {                    mShowed = true;                    showPanel(mTextPanel);                } else {                    mShowed = false;                    hidePanel(mTextPanel);                }            }        });    }    private void showPanel(View panel) {        panel.setVisibility(View.VISIBLE);    }    private void hidePanel(View panel) {        panel.setVisibility(View.GONE);    }}

效果:
点击第一个按钮,ViewStub进行显示与隐藏。

注意:开始时ViewStub是一个不可视(不占用位置)并且大小为0的视图,可以延迟到运行时填充布局资源。当ViewStub设置为Visible或调用inflate()之后,就会填充布局资源,ViewStub便会被填充的视图替代。

原创粉丝点击