Android 通过findViewById方式创建TabHost


使用TabHost可以通过继承TabHost的方式,也可以通过继承ActivityGroup的方式。

通过extends TabHost比较简单,这里就不介绍了,以下是介绍通过extends ActivityGroup的方式使用TabHost,也就是通过findViewById方式创建TabHost对象的方式,这种方式大家可能会遇到以下错误提示:

1、java.lang.IllegalStateException: Did you forget to call 'public void setup,原因是未使用setup()

2、Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup),原因是也是未正确使用setup()

3、java.lang.IllegalStateException: Activities can't be added until the containing group has been created.,原因是未继承ActivityGroup,如果只是extends Activity是不行的,必须extends ActivityGroup

4、其它错误......

原因都是因为没有按正确的方法调用TabHost,附上代码:

1、AndroidManifest.xml

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="org.shuxiang.tabhostsample"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="7"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application android:label="@string/app_name">  
  11.         <activity  
  12.             android:name=".MainActivity"  
  13.             android:label="@string/title_activity_main" >  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.   
  17.                 <category android:name="android.intent.category.LAUNCHER" />  
  18.             </intent-filter>  
  19.         </activity>  
  20.   
  21.         <activity android:name="Tab1" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">  
  22.             <intent-filter>  
  23.                 <action android:name="org.shuxiang.tabhostsample.Tab1" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.   
  27.         <activity android:name="Tab2" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">  
  28.             <intent-filter>  
  29.                 <action android:name="org.shuxiang.tabhostsample.Tab2" />  
  30.             </intent-filter>  
  31.         </activity>  
  32.       
  33.         <activity android:name="Tab3" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">  
  34.             <intent-filter>  
  35.                 <action android:name="org.shuxiang.tabhostsample.Tab3" />  
  36.             </intent-filter>  
  37.         </activity>     
  38.     </application>  
  39.   
  40. </manifest>  

layout下的布局文件:

2、tabhost.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent">      
  4.     <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">  
  5.         <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" />  
  6.         <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" />  
  7.     </LinearLayout>  
  8. </TabHost>  

3、tabhost1.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:orientation="horizontal">  
  6.      
  7.     <TextView android:id="@+id/TextView01"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第一个"  
  11.         android:padding="10px"/>  
  12.      
  13. </LinearLayout>  

4、tabhost2.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:orientation="horizontal">  
  6.      
  7.     <TextView android:id="@+id/TextView02"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第二个"  
  11.         android:padding="10px"/>  
  12.      
  13. </LinearLayout>  
5、tabhost3.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="fill_parent"  
  4.    android:layout_height="fill_parent"  
  5.    android:orientation="horizontal">  
  6.      
  7.     <TextView android:id="@+id/TextView02"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第三个"  
  11.         android:padding="10px"/>  
  12.      
  13. </LinearLayout>  

value下的文件:

6、strings.xml

  1. <resources>  
  2.     <string name="app_name">TabHostSample</string>  
  3.     <string name="hello_world">Hello world!</string>  
  4.     <string name="menu_settings">Settings</string>  
  5.     <string name="title_activity_main">MainActivity</string>  
  6. </resources>  
类文件:

7、MainActivity.java

  1. package org.shuxiang.tabhostsample;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.ActivityGroup;  
  5. import android.content.Intent;  
  6. import android.widget.TabHost;  
  7.   
  8. public class MainActivity extends ActivityGroup  
  9. {  
  10.     private String tabHost1 = "A";  
  11.     private String tabHost2 = "B";  
  12.     private String tabHost3 = "C";  
  13.     private TabHost tabHost;  
  14.   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.tabhost);  
  19.           
  20.         tabHost = (TabHost) findViewById(android.R.id.tabhost);  
  21.   
  22.         tabHost.setup(getLocalActivityManager());  
  23.           
  24.         tabHost.setCurrentTab(0);  
  25.         tabHost.addTab(tabHost.newTabSpec(tabHost1)  
  26.         .setIndicator(tabHost1).  
  27.         setContent(new Intent(MainActivity.this, Tab1.class)));  
  28.         tabHost.addTab(tabHost.newTabSpec(tabHost2)  
  29.         .setIndicator(tabHost2)  
  30.         .setContent(new Intent(MainActivity.this, Tab2.class)));  
  31.         tabHost.addTab(tabHost.newTabSpec(tabHost3)  
  32.         .setIndicator(tabHost3)  
  33.         .setContent(new Intent(MainActivity.this, Tab3.class)));  
  34.   
  35.     }  
  36.   
  37.   
  38. }  

8、Tab1.java

  1. package org.shuxiang.tabhostsample;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Tab1 extends Activity  
  7. {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.tabhost1);  
  13.     }  
  14. }  

9、Tab2.java

  1. package org.shuxiang.tabhostsample;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Tab2 extends Activity  
  7. {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.tabhost2);  
  13.     }  
  14. }  

10、Tab3.java

  1. package org.shuxiang.tabhostsample;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Tab3 extends Activity  
  7. {  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.tabhost3);  
  13.     }  
  14. }  

附上效果图:

相关内容