Android组件ViewStub基本使用方法详解

作者:lijiao 时间:2022-01-15 07:30:54 

ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:

"A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property.”

总之是:ViewStub可以给其他的view事先占据好位置,当需要的时候调用inflater()或者是setVisible()方法显示这些View组件。

layout.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

<Button
     android:id="@+id/showBtn"
     android:layout_width="match_parent"
     android:layout_height="50dip"
     android:layout_weight="1"
     android:text="show" />

<Button
     android:id="@+id/deleteBtn"
     android:layout_width="match_parent"
     android:layout_height="50dip"
     android:layout_weight="1"
     android:text="delete" />
 </LinearLayout>

<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

<ViewStub
     android:id="@+id/viewstub"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout="@layout/next" />

</LinearLayout>

</LinearLayout>

next.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" >

<ProgressBar
   android:id="@+id/progressBar1"
   style="?android:attr/progressBarStyleLarge"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

</LinearLayout>

Main.java:


package com.example.android_viewstub1;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;

public class MainActivity extends Activity {

Button btn1, btn2;
 ViewStub viewStub;

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

btn1 = (Button) this.findViewById(R.id.showBtn);
   btn2 = (Button) this.findViewById(R.id.deleteBtn);
   viewStub = (ViewStub) this.findViewById(R.id.viewstub);
   btn1.setOnClickListener(new OnClickListener() {
     @SuppressLint("NewApi")
     @Override
     public void onClick(View arg0) {
       viewStub.setVisibility(View.VISIBLE);
     }

});
   btn2.setOnClickListener(new OnClickListener() {
     @SuppressLint("NewApi")
     @Override
     public void onClick(View arg0) {
       viewStub.setVisibility(View.INVISIBLE);
     }

});

}

}

效果:

Android组件ViewStub基本使用方法详解

标签:Android,ViewStub
0
投稿

猜你喜欢

  • Treeview动态添加用户控件传值和取值的实例代码

    2021-07-20 11:20:07
  • Spring Cloud 请求重试机制核心代码分析

    2023-04-11 04:00:23
  • C#服务器NFS共享文件夹搭建与上传图片文件的实现

    2023-01-31 01:19:49
  • springboot之如何获取项目目录路径

    2022-08-09 21:44:17
  • Java注解方式之防止重复请求

    2023-05-29 16:30:51
  • Java基础语法之二维数组详解

    2023-05-09 16:49:20
  • C#实现JSON字符串序列化与反序列化的方法

    2023-12-01 12:40:23
  • C# Stream 和 byte[] 之间的转换

    2023-06-24 23:14:52
  • Java集合之Set接口及其实现类精解

    2022-01-23 17:27:44
  • 解决Android studio 2.3升级到Android studio 3.0 后apt报错问题

    2023-05-17 13:56:50
  • Java 实战图书管理系统的实现流程

    2023-12-19 05:11:13
  • Spring boot随机端口你都不会还怎么动态扩容

    2021-09-29 10:10:14
  • Java数据结构之单链表详解

    2023-11-04 17:02:20
  • C#学习笔记整理_浅谈Math类的方法

    2022-04-11 06:16:48
  • Spring整合Quartz实现定时任务调度的方法

    2023-07-07 00:55:55
  • 浅谈Spring-cloud 之 sleuth 服务链路跟踪

    2022-09-24 16:07:43
  • c语言switch反汇编的实现

    2023-06-29 03:38:17
  • 学习JVM之java内存区域与异常

    2022-07-09 09:59:41
  • 如何解决通过spring-boot-maven-plugin package失败问题

    2021-10-16 15:43:45
  • 使用cmd根据WSDL网址生成java客户端代码的实现

    2022-09-12 11:00:20
  • asp之家 软件编程 m.aspxhome.com