Android組件ViewStub基本使用方法詳解
ViewStub可以在運(yùn)行時(shí)動(dòng)態(tài)的添加布局。幫助文檔給定的定義是:
"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事先占據(jù)好位置,當(dāng)需要的時(shí)候調(diào)用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); } }); } }
效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Handler消息派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)分析了Android Handler消息派發(fā)機(jī)制源碼,感興趣的小伙伴們可以參考一下2016-07-07Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對(duì)按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-11-11Android提高之ListView實(shí)現(xiàn)自適應(yīng)表格的方法
這篇文章主要介紹了Android采用ListView實(shí)現(xiàn)自適應(yīng)表格的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08Android中EditText 設(shè)置 imeOptions 無(wú)效問(wèn)題的解決方法
有時(shí)候我們需要在EditText 輸出完之后 需要在鍵盤出現(xiàn) 右下角變成“Go”或“前往 搜索時(shí);通常我們需要設(shè)置Android:imeOptions屬性,但是今天我發(fā)現(xiàn)設(shè)置了無(wú)效,下面給大家分享下解決方案2016-12-12Android實(shí)現(xiàn)基于ZXing快速集成二維碼掃描功能
這篇文章主要為大家詳細(xì)介紹了Android二維碼掃描ZXing快速項(xiàng)目集成的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android多點(diǎn)觸摸的相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2016-08-08Android仿Keep運(yùn)動(dòng)休息倒計(jì)時(shí)圓形控件
這篇文章主要為大家詳細(xì)介紹了Android仿Keep運(yùn)動(dòng)休息倒計(jì)時(shí)圓形控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Kotlin Option與Either及Result實(shí)現(xiàn)異常處理詳解
Kotlin異常處理,異常是在程序運(yùn)行時(shí)可能發(fā)生的不必要的問(wèn)題,并突然終止您的程序。異常處理是一個(gè)過(guò)程,使用它可以防止程序出現(xiàn)可能破壞我們代碼的異常2022-12-12