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);
}
});
}
}
效果:


猜你喜欢
- 本文实例讲述了Struts2+Hibernate实现数据分页的方法。分享给大家供大家参考,具体如下:1.用Hibernate实现分页技术:/
- 我们有时用C#需要实现锁住文件的功能,该如何锁住文件呢?下面小编给大家介绍一下。首先大家需要到码云里面找到如下图所示的文件锁的项目,如下图所
- 目录说明使用常见问题No such instance field: 'logger2'说明logback作为log4j的替代
- 简介:本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件思路:主要重写Recyclerview
- SpringMVC重定向model值的获取1、步骤一:在控制器中编写/*重定向测试*/@RequestMapping("/m1/t
- Android 7.0调用相机崩溃解决办法 错误提示:android.os.FileUriExposedException: fi
- 一、判断语句最常用的顺序结构只能顺序执行,并不能进行判断和选择。于是便有了下面两种分支结构if语句switch语句1. if语句一个if语句
- springboot和springmvc的区别spring boot 内嵌tomcat,Jetty和Undertow容器,可以直接运行起来,
- 一、前言上一篇文章中我们已经Spring Boot 利用注解方式整合 MyBatis,今天我们就来看看,如何利
- 一 介绍本节给知识追寻者给大家带来的是springSecurity入门篇,主要是简述下springSecrurity的启动原理和简单的入门搭
- 将int数组转化为Integer数组这里使用java8的stream来进行转化,详细步骤如下所示://初始化int数组int[] nums
- 一、题目描述题目实现:使用网络编程时,需要通过Socket传递对象。二、解题思路创建一个类:Student,实现序列化Student类包含两
- List查询JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种
- 最近接触了Android自定义控件,涉及到自定义xml中得属性(attribute),其实也很简单,但是写着写着,发现代码不完美了,就是在a
- 今天因为工作需要,把以前编写的一个GPS测试程序拿出来重新修改了一下。这个程序说起来有些历史了,是我11年编写的,那时候学了Android开
- 写在前面之前想尝试把JWT和Shiro结合到一起,但是在网上查了些博客,也没太有看懂,所以就自己重新研究了一下Shiro的工作机制,然后自己
- 一个界面,实现在向页面添加图片时,在标题上显示一个水平进度条,当图片载入完毕后,隐藏进度条并显示图片具体实现方法:res/layout/ma
- Spring的最基本的能力就是DI,即依赖注入,或控制反转,它可以为Bean注入其依赖的其他Bean。一个Bean依赖其他Bean一般是通过
- 前言等待总是让人感到焦急和厌烦的,特别是看不到进展的等待。所以为了不让用户痴痴地等,我们在进行某些耗时操作时,一般都要设计一个进度条或者倒计
- 最近正式入坑Flutter,首先从环境搭建开始,看了网上好多关于Windows环境搭建的资料,基本都是按官方文档写的,看完的感受是,还不如直