Android之tabhost佈局

1.概念

盛放Tab的容器就是TabHost。TabHost的實現有兩種方式:

第一種繼承TabActivity,從TabActivity中用getTabHost()方法獲取TabHost。各個Tab中的內容在佈局文件中定義就行了。

第二種方式,不繼承TabActivity,在佈局文件中定義TabHost即可,但是TabWidget的id必須是@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent。

2.案例

1)繼承TabActivity

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
 <!-- 定義TabHost組件 -->
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
   <!-- 定義第一個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <!-- 定義兩個TextView用於顯示標籤頁中的內容 -->
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孫悟空-2011/07/12"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="豬八戒-2011/07/10"/>
   </LinearLayout>
   <!-- 定義第二個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="薩僧-2011/07/11"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="唐僧-2011/07/10"/>
   </LinearLayout>
   <!-- 定義第三個標籤頁的內容 -->
   <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孫悟空-2011/07/12"/> 
     <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="薩僧-2011/07/08"/>
   </LinearLayout>
 </TabHost>

HelloTabHost.java

 
public class HelloTabHost extends TabActivity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 
     //調用TabActivity的getTabHost()方法獲取TabHost對象
     TabHost tabHost = getTabHost();
 
     //設置使用TabHost佈局
     LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
 
     //添加第一個標籤頁
     tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接電話").setContent(R.id.tab01));
 
     //添加第二個標籤頁,並在其標籤上添加一個圖片
     tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接電話",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
 
     //添加第三個標籤頁
     tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已撥電話").setContent(R.id.tab03));
   }
 }


運行效果:

2)不繼承TabActivity

繼承普通Activity,<TabWidget>標籤id必須爲tabs、<FrameLayout>標籤id必須爲tabcontent.這個方式在通過findViewById獲得TabHost之後,必須要調用setup方法。

main.xml代碼
<?xml version="1.0" encoding="utf-8"?>  
  <!-- TabHost必須包含一個 TabWidget和一個FrameLayout-->   
  <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content">  
        <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">  
            <!-- TabWidget的id屬性必須爲 @android:id/tabs-->              
            <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent"  
              android:layout_height="wrap_content" />  
            <!-- FrameLayout的id屬性必須爲 @android:id/tabcontent-->  
             <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">  
                <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
                <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
                <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/>  
             </FrameLayout>  
         </LinearLayout>  
    </TabHost>  
</LinearLayout> 

Java代碼

public class TabHostTest extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // 獲取TabHost對象  
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        // 如果沒有繼承TabActivity時,通過該種方法加載啓動tabHost  
        tabHost.setup();  
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標籤",  
                getResources().getDrawable(R.drawable.icon)).setContent(  
                R.id.view1));  
  
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標籤")  
                .setContent(R.id.view3));  
  
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標籤")  
                .setContent(R.id.view2));  
    }  
} 


效果圖


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章