创建TabHost有两种方式
第一种:继承TabActivity,使用getTabHost初始化。
第二种:继承Activity,使用this.findViewById(R.id.tabhost)初始化。
但是,当我们的setContent()中的参数是Intent时,也就是他的tab包含的是Activity而不是其他的布局或者组件时,之前正常的代码会发生错误,这里我说一下一点总结吧
第一,继承Activity
public class TabDemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // 注意下面这段 LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false); mLocalActivityManager.dispatchCreate(savedInstanceState); tabHost.setup(mLocalActivityManager); tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1") .setContent(new Intent(this, FirstActivity.class))); tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2") .setContent(new Intent(this, SecondActivity.class))); tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3") .setContent(new Intent(this, ThirdlyActivity.class))); } }
public class TabDemoActivity extends ActivityGroup { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // 注意下面这段 tabHost.setup(this.getLocalActivityManager()); tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1") .setContent(new Intent(this, FirstActivity.class))); tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2") .setContent(new Intent(this, SecondActivity.class))); tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3") .setContent(new Intent(this, ThirdlyActivity.class))); } }
为什么需要调用setup方法,官方给出的解释,setup方法主要是初始化了TabConnect和TabWidget
If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.
上面这两种代码的对应XML,注意除了tabhost的id可以自定义外,其他的必须使用系统的id
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:padding="5dp" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" > </TabWidget> </LinearLayout> </TabHost>
public class TabDemoActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); // 注意id TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.addTab(tabHost.newTabSpec("one").setIndicator("tab1") .setContent(new Intent(this, FirstActivity.class))); tabHost.addTab(tabHost.newTabSpec("two").setIndicator("tab2") .setContent(new Intent(this, SecondActivity.class))); tabHost.addTab(tabHost.newTabSpec("three").setIndicator("tab3") .setContent(new Intent(this, ThirdlyActivity.class))); } }
第三种使用的XML如下
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:padding="5dp" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" > </TabWidget> </LinearLayout> </TabHost>