广告
返回顶部
首页 > 资讯 > 移动开发 >Android组件ViewStub基本使用方法详解
  • 655
分享到

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

方法viewstubAndroid 2022-06-06 08:06:47 655人浏览 八月长安
摘要

ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是: "A ViewStub is an invisible, zero-sized View that can

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抽象布局——include、merge 、ViewStub详解Android编程之include文件的使用方法Android中使用include标签和merge标签重复使用布局android使用include调用内部组件的方法解析android中include标签的使用Android开发之merge结合include优化布局Android开发技巧之ViewStub控件惰性装载深入分析Android ViewStub的应用详解Android布局技巧之include、merge与ViewStub标签的巧用


--结束END--

本文标题: Android组件ViewStub基本使用方法详解

本文链接: https://www.lsjlt.com/news/24889.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作