ViewStub应用

在开发应用程序的时候,会遇到这样的情况,在运行时动态的根据条件来决定显示哪个View或哪个布局,可以把可能用到的View都写在上面,先把他们的可见性设置为View.GONE,然后在代码中动态的更改它的可见性,这个做法优点是逻辑简单且控制起来比较灵活,缺点是耗费资源。

另外,我们可以使用ViewStub,ViewStub是一个轻量级的View,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate实例化,然后ViewStub的布局属性就会传给他所指向的布局。

特点:

1.ViewStub只能Inflate一次,即某个ViewStub指定的布局被Inflate后,就不能再通过ViewStub来控制他了

2.ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然可以吧View卸载某个布局文件中

 

activity_main.xml

<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">  
<Button 
        android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/show AAA""/>
 <Button 
        android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/show BBB"/>

  <ViewStub   
    android:id="@+id/aaa"  
    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/aaa"/>

<ViewStub   
    android:id="@+id/bbb"  
    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/bbb"/> 
</LinearLayout>

aaa.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" >
    <TextView         
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AAA"/>

</LinearLayout>

bbb.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" >
    <TextView 
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BBB"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    Button btn1;
    Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ViewStub stub=(ViewStub) findViewById(R.id.aaa);
                stub.inflate();
            }
        });
        btn2=(Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ViewStub stub=(ViewStub) findViewById(R.id.bbb);
                stub.inflate();
            }
        });
        
    }

}

 

注意事项:

某些不技术型要加载ViewStub上才会起作用,加到实际布局上不会起作用,例如android:layout_margin*系列属性,如果加在TextView上不会起作用,需要加载ViewStub上,而ViewStub的属性在inflate()会都传给相应的布局

 

posted @ 2016-03-04 16:28  ChHM  阅读(191)  评论(0编辑  收藏  举报