Android 通过findViewById方式创建TabHost
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
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.shuxiang.tabhostsample"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="15" />
- <application android:label="@string/app_name">
- <activity
- android:name=".MainActivity"
- android:label="@string/title_activity_main" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name="Tab1" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
- <intent-filter>
- <action android:name="org.shuxiang.tabhostsample.Tab1" />
- </intent-filter>
- </activity>
- <activity android:name="Tab2" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
- <intent-filter>
- <action android:name="org.shuxiang.tabhostsample.Tab2" />
- </intent-filter>
- </activity>
- <activity android:name="Tab3" android:launchMode="singleInstance" android:screenOrientation="user" android:configChanges="orientation|keyboardHidden">
- <intent-filter>
- <action android:name="org.shuxiang.tabhostsample.Tab3" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
layout下的布局文件:
2、tabhost.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost android:id="@android:id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
- <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" />
- <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" />
- </LinearLayout>
- </TabHost>
3、tabhost1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <TextView android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第一个"
- android:padding="10px"/>
- </LinearLayout>
4、tabhost2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <TextView android:id="@+id/TextView02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第二个"
- android:padding="10px"/>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <TextView android:id="@+id/TextView02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第三个"
- android:padding="10px"/>
- </LinearLayout>
value下的文件:
6、strings.xml
- <resources>
- <string name="app_name">TabHostSample</string>
- <string name="hello_world">Hello world!</string>
- <string name="menu_settings">Settings</string>
- <string name="title_activity_main">MainActivity</string>
- </resources>
7、MainActivity.java
- package org.shuxiang.tabhostsample;
- import android.os.Bundle;
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.widget.TabHost;
- public class MainActivity extends ActivityGroup
- {
- private String tabHost1 = "A";
- private String tabHost2 = "B";
- private String tabHost3 = "C";
- private TabHost tabHost;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabhost);
- tabHost = (TabHost) findViewById(android.R.id.tabhost);
- tabHost.setup(getLocalActivityManager());
- tabHost.setCurrentTab(0);
- tabHost.addTab(tabHost.newTabSpec(tabHost1)
- .setIndicator(tabHost1).
- setContent(new Intent(MainActivity.this, Tab1.class)));
- tabHost.addTab(tabHost.newTabSpec(tabHost2)
- .setIndicator(tabHost2)
- .setContent(new Intent(MainActivity.this, Tab2.class)));
- tabHost.addTab(tabHost.newTabSpec(tabHost3)
- .setIndicator(tabHost3)
- .setContent(new Intent(MainActivity.this, Tab3.class)));
- }
- }
8、Tab1.java
- package org.shuxiang.tabhostsample;
- import android.app.Activity;
- import android.os.Bundle;
- public class Tab1 extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabhost1);
- }
- }
9、Tab2.java
- package org.shuxiang.tabhostsample;
- import android.app.Activity;
- import android.os.Bundle;
- public class Tab2 extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabhost2);
- }
- }
10、Tab3.java
- package org.shuxiang.tabhostsample;
- import android.app.Activity;
- import android.os.Bundle;
- public class Tab3 extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabhost3);
- }
- }
附上效果图:
评论暂时关闭