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

嵌套Tabhost

2014年01月24日 ⁄ 综合 ⁄ 共 3452字 ⁄ 字号 评论关闭

今天讲一下,如何在TabHost中,再放TabHost。

 

先来看一下效果。

 

一层TabHost

 

 

两层Tabhost (内部TabHots在上面)

 

两层TabHost (内层TabHots在下面)

 

 

 

下面说一下代码,一共3个 java类,3个xml布局文件。

 

看一下主画面:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="@drawable/default_bg">  
  6.     <LinearLayout android:orientation="vertical"   
  7.         android:layout_width="fill_parent" android:layout_height="fill_parent">  
  8.         <TabWidget android:id="@android:id/tabs"  
  9.             android:layout_alignParentBottom="true" android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content"/>  
  11.         <FrameLayout android:id="@android:id/tabcontent"  
  12.             android:layout_weight="1" android:layout_width="fill_parent"  
  13.             android:layout_height="fill_parent" />  
  14.     </LinearLayout>  
  15. </TabHost>  

 

 

就是常规的Tabhost布局。

 

入口类:

DoubleTabHost.java

  1. package com.yfz;  
  2. import android.app.TabActivity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.widget.TabHost;  
  6. /** 
  7.  * 本类继承了TabActivity 
  8.  * @author Administrator 
  9.  * 
  10.  */  
  11. public class DoubleTabHost extends TabActivity {  
  12.       
  13.      /* 注意: 
  14.      * 对于TabHost、布局文件中必须包含 
  15.      * TabHost、TabWidget 、FrameLayout  
  16.      * 如果继承TabActivity,并且通过getTabHost()方法来获取TabHost 
  17.      * 那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent 
  18.      *  
  19.      * 如果继承Activity,可以通过findViewById来获取这三个组件,此时ID可自定义 
  20.      */  
  21.       
  22.     /** Called when the activity is first created. */  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         TabHost mTabHost = getTabHost();  
  29.         mTabHost.addTab(mTabHost.newTabSpec("Twitter").setIndicator(  
  30.                 "Twitter",  
  31.                 getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(  
  32.                 new Intent(this, SubTab.class)));  
  33.         mTabHost.addTab(mTabHost.newTabSpec("Facebook").setIndicator(  
  34.                 "Facebook",  
  35.                 getResources().getDrawable(android.R.drawable.arrow_down_float)).setContent(  
  36.                 new Intent(this, NormalActivity.class)));  
  37.         mTabHost.setCurrentTab(0);  
  38.     }  
  39. }  

 

 

 

对于TabHost、布局文件中必须包含TabHost、TabWidget 、FrameLayout .缺一不可

如果加载该TabHost画面的类继承TabActivity,并且想通过getTabHost()方法来获取TabHost,getTabWidget()方法获取TabWidget,

那么TabHost、TabWidget 、FrameLayout 三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent

 

 

否则会报运行时异常,错误如下:

TabHost ID错误:

  1. ERROR/AndroidRuntime(8301): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'  

 

 

TabWidget ID 错误:

  1. ERROR/AndroidRuntime(8354): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'  

 

 

FrameLayout  ID错误:

  1. ERROR/AndroidRuntime(8404): Caused by: java.lang.RuntimeException: Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'  

 

 

 

子TabHost页面:

subtab.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/mytabhost" android:layout_width="fill_parent

抱歉!评论已关闭.