欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-01-05 13:46:43|栏目:Android代码|点击:

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解析服务器端发来的xml数据示例

栏    目:Android代码

下一篇:浅谈React Native打包apk的坑

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

本文地址:http://www.codeinn.net/misctech/40878.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有