ViewStub的实现

一个例子

点击按钮前


点击按钮后


UI布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <TextView
        android:id="@+id/title"
        android:background="#aaccaa"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"/>
    <ViewStub
        android:id="@+id/viewstub1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout="@layout/viewstub_layout"/>
    <TextView
        android:id="@+id/title_below_viewstub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ccaaaa"
        android:text="ViewStub下面的控件" />

</LinearLayout>

viewstub_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#cccccc"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <TextView
        android:id="@+id/viewstub1_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="得意不忘形,失意不失志"
        android:textSize="16sp"/>
</LinearLayout>

代码

package com.jue.myviewstub;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTitle;
    private Button button;

    private ViewStub mViewStub;
    private TextView mViewStubTitle;

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

        mTitle = (TextView) findViewById(R.id.title);
        mTitle.setText("");
        button = (Button) findViewById(R.id.show);
        button.setOnClickListener(this);

        setTitleInfo();
    }

    @Override
    public void onClick(View v) {
        mViewStub.inflate();

        setTitleInfo();
    }

    private void setTitleInfo() {
        mViewStub = (ViewStub) findViewById(R.id.viewstub1);
        mViewStubTitle = (TextView) findViewById(R.id.viewstub1_title);

        String title1 = mViewStub == null ? "ViewStub 空--" : "ViewStub  非空++";
        String title2 = mViewStubTitle == null ? "ViewStub 子View 空--" : "ViewStub 子View  非空++";


        mTitle.setText(title1 + "\n" + title2);
    }
}

可以发现

  1. 点击按钮之前:
    • ViewStub 关联到的子TextView 为null。
    • ViewStub控件不为null。
  2. 点击按钮之后:
    • ViewStub 关联到的子TextView 为不为null。
    • ViewStub控件 为null。
  3. ViewStub开始时并没有显示。

ViewStub的实现

继承自View


开始时,ViewStub并没有显示

因为在构造器里已经设置了GONE。


调用inflate时


可知:

使用ViewStub时,会首先把当前View(仅是当前View)添加到父容器。当调用inflate方法时,会把layout指定的xml inflate出来,然后移除当前View,把刚才inflate出来的view返回。

通过文档

setVisibility方法也可以完成对ViewStub的替换。查看代码,当未调用inflate时,mInflatedViewRef为空,会走进inflate调用,因此,setVisibility(View.Visible),也可以起到layout对ViewStub的替换。


建议:

当inflate执行之后,实际上ViewStub已经从父容器中移除,当前只有mViewStub引用到,并且不再使用。最好把mViewStub赋值为null。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值