Android viewStub应用

1.viewstub是一个轻量级的 view,他是一个看不见的,不占布局位置,占用资源很小的控件。java

能够为viewstub指定一个布局,在inflate布局的时候,只有viewstub会被初始化。而后当viewstub被设置为可见的时候,或者调用了inflate()的时候,viewstub所指向的布局就会被inflate和实例化,而后viewstub的布局属性都会传给所指向的布局,这样子,就能够使用viewstub来方便在运行时,有选择的显示某一个布局android

viewstub特色:app

他只能inflate一次,以后viewstub对象会被设置为空换句话说,某一个被viewstub指定的布局被inflate以后,这个ViewStub就从View层次中移除了,就不会再经过viewstub来控制他了dom

viewstub只能用来inflate一个布局文件,而不是某个具体的view,固然也能够吧某个view写在某个布局文件中ide

 viewstub_main.xml布局

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center_horizontal">
  <ViewStub 
    android:id="@+id/viewstub_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout_marginTop="10dip"
    android:layout="@layout/viewstub_textview_layout"/>
  <ViewStub 
    android:id="@+id/viewstub_iamgeview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    android:layout="@layout/viewstub_imageview_layout"/>
</LinearLayout>

 当你准备inflate ViewStub时,调用inflate()方法便可。你还能够设定ViewStubVisibilityVISIBLEINVISIBLE,也会触发inflate。注意的是,使用inflate()方法能返回布局文件的根View
viewstub_textview_layout.xmlspa

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <TextView
        android:id="@+id/viewstub_demo_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#aa664411"
        android:textSize="16sp"/>
</LinearLayout>

viewstub_imageview_layout.xmlcode

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/viewstub_demo_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

代码:xml

 

package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewStubDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewstub_main);
        if ((((int) (Math.random() * 100)))>50) {
            // to show text
            // all you have to do is inflate the ViewStub for textview
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_textview);
            stub.inflate();
            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);
            text.setText("我如今心情很很差,有的时候人是否是应该对本身狠一点," +
            		"或许真的应该来一个破釜沉舟,人生就是一个棋局,每一步都是一" +
            		"个赌博!失去一个棋子,只要将帅安在,不到最后,也不能判定就是输家");
        } else {
            // to show image
            // all you have to do is inflate the ViewStub for imageview
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_iamgeview);
            stub.inflate();
            ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);
            image.setImageResource(R.drawable.gallery_01);
        }
    }
}

结果:对象

随机数大于0.5小于1显示的结果:

随机数大于0小于0.5显示的结果:

相关文章
相关标签/搜索