国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android組件ViewStub基本使用方法詳解

2019-12-12 06:17:48
字體:
供稿:網(wǎng)友

ViewStub可以在運行時動態(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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天等县| 藁城市| 青神县| 广东省| 丰台区| 正镶白旗| 平江县| 米脂县| 凤冈县| 教育| 商洛市| 荣成市| 滦平县| 秭归县| 偏关县| 新蔡县| 九寨沟县| 台北县| 民乐县| 本溪| 昭苏县| 揭西县| 庐江县| 多伦县| 灵武市| 青海省| 陵水| 张家港市| 翼城县| 郓城县| 密云县| 应城市| 天水市| 正蓝旗| 肇源县| 苍山县| 延川县| 揭西县| 白玉县| 巴彦县| 陇川县|