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开发之Notification通知用法详解
下一篇: XHTML标准的版本