ViewStub

 ViewStub和Include标签都是实现UI重用,区别在ViewStub在你需要时渲染主界面。可以通过inflate,setvisibility方法让ViewStub渲染的UI上。

stub.xml文件

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.   
  3. <LinearLayout   
  4.     xmlns:android = "http://schemas.android.com/apk/res/android"   
  5.     android:layout_width = "match_parent"   
  6.     android:layout_height = "match_parent"  
  7.     android:tag="stub" > 
  8.     <TextView android:text="TextView"  
  9.     android:id="@+id/textView1"  
  10.     android:layout_width="wrap_content" 
  11.     android:layout_height="wrap_content"></TextView>   
  12. </LinearLayout>   

main2.xml文件

  
  
  
  
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:padding="6dip"> 
  5.      
  6.     <ImageView 
  7.         android:id="@+id/icon" 
  8.          
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.          
  12.         android:layout_alignParentBottom="true" 
  13.         android:layout_marginRight="6dip" 
  14.         android:src="@drawable/icon" /> 
  15.  
  16.     <Button android:id="@+id/button1" 
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_above="@id/icon" 
  20.          android:layout_marginRight="6dip" 
  21.         android:text="button1"/> 
  22.     <ViewStub 
  23.         android:id="@+id/stub"  
  24.         android:tag="viewStub" 
  25.         android:layout="@layout/stub" 
  26. android:inflateId="@+id/root" //该属性指重写stub.xml中root view 的id
  27.         android:layout_above="@id/button1" 
  28.          android:layout_marginRight="6dip" 
  29.         android:layout_height="wrap_content"  
  30.         android:layout_width="wrap_content"  
  31.     /> 
  32.     <Button  
  33.         android:id="@+id/button2" 
  34.         android:layout_above="@id/stub" 
  35.         android:layout_width="wrap_content"  
  36.          android:layout_marginRight="6dip" 
  37.         android:layout_height="wrap_content"  
  38.         android:text="button2"/> 
  39.  
  40. </RelativeLayout> 

code

 

  
  
  
  
  1. public class LayoutActivity extends Activity implements OnClickListener{ 
  2.     /** Called when the activity is first created. */ 
  3.      
  4.     Button button1; 
  5.     //View vs; 
  6.     ViewStub vs; 
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main2); 
  11.         button1 = (Button)findViewById(R.id.button1); 
  12.        // vs = ((ViewStub)findViewById(R.id.stub)).inflate(); 
  13.         vs = ((ViewStub)findViewById(R.id.stub)); 
  14.         button1.setOnClickListener(this); 
  15.     } 
  16.     @Override 
  17.     public void onClick(View v) { 
  18.         // TODO Auto-generated method stub 
  19.         vs.inflate(); 
  20.         //vs.inf 
  21.         Log.e("will", ""+vs.getTag()); 
  22.         //vs.setVisibility(View.VISIBLE); 
  23.     } 

启动后界面截图

单击Button1后

注意:vs.setVisibility和vs.inflate()方法都可以让ViewStub inflat 被包含的UI,通过sdk tools下hierarchy工具可以看出,当ViewStub 被inflate后,ViewStub会在hierarchy图中移除,因此没有必要长期保存和ViewStub有关的引用,看sdk中是这样描述的

It is very important to remember that after the stub is inflated, the stub is removed from the view hierarchy. As such, it is unnecessary to keep a long-lived reference, for instance in an class instance field, to a ViewStub.

ViewStub is a great compromise between ease of programming and efficiency. Instead of inflating views manually and adding them at runtime to your view hierarchy, simply use a ViewStub. It's cheap and easy. The only drawback of ViewStub is that it currently does not support the <merge /> tag.

你可能感兴趣的:(职场,ViewStub,休闲)