摘要:Android - ViewStub
參考:Android實戰技巧:ViewStub的應用
http://blog.csdn.net/hitlion2008/article/details/6737537
因為不希望一個layout裡,有大量的xml標籤,導致layout管理的不便,
所以將一個一個資訊,切出去xml片段。
然後使用viewstub引入。
再由程式碼做設定
這樣會讓layout看起來滿舒服的,
而且這個Layout片段,就可以在不同的地方重複使用,是會比FragmentLayout 輕量,主要是xml的大量複製,但不會有程式碼片段的複製。
或許比較適合介面相同,資訊不同的程式處理
實作如下:
先建立viewstub_test.xml ,內容跟一般的layout xml一樣,不多作撰寫
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical" >
<LinearLayout 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:padding="5dp">
   <TextView 
       android:text="@string/name"
       style="@style/title"/>
   <TextView
       android:id="@+id/stub_name"
       style="@style/content"/>
</LinearLayout>
</LinearLayout>
	
	程式套用引入該xml片段如下
<viewstub 
android:id="@+id/viewstub_test" 
android:layout="@layout/viewstub_test" 
android:layout_height="wrap_content"
 android:layout_marginleft="5dip" 
android:layout_marginright="5dip" 
android:layout_margintop="10dip" 
android:layout_width="match_parent"> 
</viewstub>
	
程式面使用如下
           ViewStub stub = (ViewStub) view.findViewById(R.id.viewstub_test);
            //載入
            stub.inflate();  
            TextView name = (TextView) view.findViewById(R.id.stub_name);