Android组件ViewStub基本使用方法详解
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中EditText 设置 imeOptions 无效问题的解决方法
有时候我们需要在EditText 输出完之后 需要在键盘出现 右下角变成“Go”或“前往 搜索时;通常我们需要设置Android:imeOptions属性,但是今天我发现设置了无效,下面给大家分享下解决方案2016-12-12Android编程开发之多点触摸(Multitouch)实现方法
这篇文章主要介绍了Android编程开发之多点触摸(Multitouch)实现方法,结合实例形式详细分析了Android多点触摸的相关实现步骤与操作技巧,需要的朋友可以参考下2016-08-08Kotlin Option与Either及Result实现异常处理详解
Kotlin异常处理,异常是在程序运行时可能发生的不必要的问题,并突然终止您的程序。异常处理是一个过程,使用它可以防止程序出现可能破坏我们代码的异常2022-12-12
最新评论