歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 通過findViewById方式創建TabHost

Android 通過findViewById方式創建TabHost

日期:2017/3/1 10:14:58   编辑:Linux編程

使用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. <uses-sdk
  6. android:minSdkVersion="7"
  7. android:targetSdkVersion="15" />
  8. <application android:label="@string/app_name">
  9. <activity
  10. android:name=".MainActivity"
  11. android:label="@string/title_activity_main" >
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. <activity android:name="Tab1" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
  18. <intent-filter>
  19. <action android:name="org.shuxiang.tabhostsample.Tab1" />
  20. </intent-filter>
  21. </activity>
  22. <activity android:name="Tab2" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
  23. <intent-filter>
  24. <action android:name="org.shuxiang.tabhostsample.Tab2" />
  25. </intent-filter>
  26. </activity>
  27. <activity android:name="Tab3" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
  28. <intent-filter>
  29. <action android:name="org.shuxiang.tabhostsample.Tab3" />
  30. </intent-filter>
  31. </activity>
  32. </application>
  33. </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. <TextView android:id="@+id/TextView01"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="第一個"
  10. android:padding="10px"/>
  11. </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. <TextView android:id="@+id/TextView02"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="第二個"
  10. android:padding="10px"/>
  11. </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. <TextView android:id="@+id/TextView02"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="第三個"
  10. android:padding="10px"/>
  11. </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. import android.os.Bundle;
  3. import android.app.ActivityGroup;
  4. import android.content.Intent;
  5. import android.widget.TabHost;
  6. public class MainActivity extends ActivityGroup
  7. {
  8. private String tabHost1 = "A";
  9. private String tabHost2 = "B";
  10. private String tabHost3 = "C";
  11. private TabHost tabHost;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.tabhost);
  16. tabHost = (TabHost) findViewById(android.R.id.tabhost);
  17. tabHost.setup(getLocalActivityManager());
  18. tabHost.setCurrentTab(0);
  19. tabHost.addTab(tabHost.newTabSpec(tabHost1)
  20. .setIndicator(tabHost1).
  21. setContent(new Intent(MainActivity.this, Tab1.class)));
  22. tabHost.addTab(tabHost.newTabSpec(tabHost2)
  23. .setIndicator(tabHost2)
  24. .setContent(new Intent(MainActivity.this, Tab2.class)));
  25. tabHost.addTab(tabHost.newTabSpec(tabHost3)
  26. .setIndicator(tabHost3)
  27. .setContent(new Intent(MainActivity.this, Tab3.class)));
  28. }
  29. }

8、Tab1.java

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

9、Tab2.java

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

10、Tab3.java

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

附上效果圖:

Copyright © Linux教程網 All Rights Reserved