现在的位置: 首页 > 综合 > 正文

Android ViewStub的用法

2017年12月18日 ⁄ 综合 ⁄ 共 4046字 ⁄ 字号 评论关闭

大家写项目的时候肯定会有一些东西提前写好,但是不到一定条件是不想让它显示出来的,我们可能的做法就是让它View.GONE
View.INVISIBLE等到一定条件了在代码里面这设置
View.VISIBLE
虽然这样写起来也挺方便的,也符合逻辑,但是这个条件是八百年都不出一次,你这么写就有点大才小用了,因为你事先布局好的
Inflate,也就是说也就是说仍然会创建对象,会被实例化,会被设置属性。也就是说,会耗费内存等资源。

推荐的做法是使用android.view.ViewStub,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。可以为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。这样,就可以使用ViewStub来方便的在运行时,要还是不要显示某个布局。

但ViewStub也不是万能的,下面总结下ViewStub能做的事儿和什么时候该用ViewStub,什么时候该用可见性的控制。

     首先来说说ViewStub的一些特点:

         1. ViewStub只能Inflate一次,之后ViewStub对象会被置为空。按句话说,某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控制它了。

         2. ViewStub只能用来Inflate一个布局文件,而不是某个具体的View,当然也可以把View写在某个布局文件中。

     基于以上的特点,那么可以考虑使用ViewStub的情况有:

         1. 在程序的运行期间,某个布局在Inflate后,就不会有变化,除非重新启动。

              因为ViewStub只能Inflate一次,之后会被置空,所以无法指望后面接着使用ViewStub来控制布局。所以当需要在运行时不止一次的显示和隐藏某个布局,那么ViewStub是做不到的。这时就只能使用View的可见性来控制了。

         2. 想要控制显示与隐藏的是一个布局文件,而非某个View。

              因为设置给ViewStub的只能是某个布局文件的Id,所以无法让它来控制某个View。

     所以,如果想要控制某个View(如Button或TextView)的显示与隐藏,或者想要在运行时不断的显示与隐藏某个布局或View,只能使用View的可见性来控制。

下面就说一个例子,这个例子是这样的,我们先用ViewStub把xml布好,这显然不会被Inflate,我们在代码让他显示出来,但是突然条件有不符合了,我必须把显示的东西还得把它隐藏掉,但是这个时候ViewStub已经被销毁了,已经不能再用了,这个时候我们还得用GONE和VISIBLE来控制显示和隐藏。

首先我们定于三个XML:

主xml:

<RelativeLayout 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"
    tools:context=".MainActivity" >

	<ViewStub 
	android:id="@+id/viewstub1"  
    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/layout1"/>
	<ViewStub 
	android:id="@+id/viewstub2"  
    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/layout2"  />

	<Button
	    android:id="@+id/btn_show"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_alignParentBottom="true"
	    android:layout_centerHorizontal="true"
	    android:text="点击我显示隐藏的布局" />

</RelativeLayout>

剩下就是需要加载的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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是被隐藏起来的布局" />

</LinearLayout>
<?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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

我们看主代码:

package com.example.viewstub;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ViewStub viewStub1;
	private ViewStub viewStub2;
	private Button btnShow;
	private boolean b=true;
	private boolean a=true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		
		
		
	}
	private void initView(){
	viewStub1=(ViewStub) findViewById(R.id.viewstub1);
	viewStub2=(ViewStub) findViewById(R.id.viewstub2);
	btnShow=(Button) findViewById(R.id.btn_show);
	btnShow.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(b){//条件符合 显示布局
				viewStub1.inflate();
				viewStub2.inflate();
				b=false;
			}else{
				if(a){//条件不符合 这个viewStub已经回收了所以我们只能用GONE和VISIBLE
					viewStub2.setVisibility(View.GONE);
					viewStub1.setVisibility(View.GONE);
					a=false;
				}else{//条件符合
					viewStub2.setVisibility(View.VISIBLE);
					viewStub1.setVisibility(View.VISIBLE);
					a=true;
				}
				
			}
			
		}
	});
	
	}
	
	
}

这样比较试用八百年都出现的条件符合。
代码太简单,不贴效果了!



抱歉!评论已关闭.