Android組件ViewStub基本使用方法詳解
ViewStub可以在運(yùn)行時動態(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)需要的時候調(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);
}
});
}
}
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之桌面小部件AppWidgetProvider用法示例
這篇文章主要介紹了Android編程之桌面小部件AppWidgetProvider用法,結(jié)合具體實例形式分析了Android桌面組件AppWidgetProvider的功能、布局、權(quán)限設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Activity取消界面切換的默認(rèn)動畫方法(推薦)
下面小編就為大家?guī)硪黄狝ctivity取消界面切換的默認(rèn)動畫方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Android使用WebView實現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android使用WebView實現(xiàn)文件下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android Cocos Creator游戲開發(fā)平臺打包優(yōu)化實現(xiàn)方案
Cocos Creator是一款輕量、高效、免費開源的跨平臺游戲引擎,同時也是實時3D內(nèi)容創(chuàng)作平臺,不僅支持2D、3D的游戲開發(fā),同時在HMI、IoT、XR、虛擬人偶等領(lǐng)域,均可提供一套完善的行業(yè)解決方案2022-11-11
Android學(xué)習(xí)教程之動態(tài)GridView控件使用(6)
這篇文章主要為大家詳細(xì)介紹了Android動態(tài)GridView控件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android Intent調(diào)用 Uri的方法總結(jié)
這篇文章主要介紹了Android Intent調(diào)用 Uri的方法總結(jié)的相關(guān)資料,這里整理了Android Intent 調(diào)用Uri的常用方法,需要的朋友可以參考下2017-09-09
android通過okhttpClient下載網(wǎng)頁內(nèi)容的實例代碼
本篇文章主要介紹了android通過okhttpClient下載網(wǎng)頁內(nèi)容的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android Webview添加網(wǎng)頁加載進(jìn)度條實例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進(jìn)度條實例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01

