是我们要用
mainTabSpec.setContent(new Intent(this,MainTabActivity.class));
因为setContent(括号里是一个intint)
所以要设置
tabHost.setup(LocalActivityManager的类);
为了得到LocalActivityManager类
就必须使用
this.getLocalActivityManager()方法
而这个方法必须要当前类继承ActivityGroup
这里是MyActivity类
public class MyActivity extends ActivityGroup {
private static final String HOME_TAB = "home";
private static final String AT_TAB = "at";
private static final String MSG_TAB = "msg";
private static final String SEARCH_TAB = "search";
private static final String MENU_TAB = "menu";
private TabHost tabHost;
private RadioGroup radioGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost)findViewById(R.id.main_tab_host);
//初始化tabHost 传递LocalActivityManager
tabHost.setup(this.getLocalActivityManager());
//穿件 TabSpec
TabHost.TabSpec mainTabSpec = tabHost.newTabSpec(HOME_TAB);
TabHost.TabSpec atTabSpec = tabHost.newTabSpec(AT_TAB);
TabHost.TabSpec msgTabSpec = tabHost.newTabSpec(MSG_TAB);
TabHost.TabSpec searchTabSpec = tabHost.newTabSpec(SEARCH_TAB);
TabHost.TabSpec moreTabSpec = tabHost.newTabSpec(MENU_TAB);
//设置TabSpec 对应的activity
mainTabSpec.setIndicator(HOME_TAB).setContent(new Intent(this,MainTabActivity.class));
atTabSpec.setIndicator(AT_TAB).setContent(new Intent(this,AtTabActivity.class));
msgTabSpec.setIndicator(MSG_TAB).setContent(new Intent(this,MenuTabActivity.class));
searchTabSpec.setIndicator(SEARCH_TAB).setContent(new Intent(this,SearchTabActivity.class));
moreTabSpec.setIndicator(MENU_TAB).setContent(new Intent(this,MenuTabActivity.class));
//添加到mTabSpecs List里
tabHost.addTab(mainTabSpec);
tabHost.addTab(atTabSpec);
tabHost.addTab(msgTabSpec);
tabHost.addTab(searchTabSpec);
tabHost.addTab(moreTabSpec);
//获取radioGroup
radioGroup = (RadioGroup) findViewById(R.id.rg_main_btns);
//radioGroup监听事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
//通过tag的名字来设置当前面
case R.id.rd_btn_main:
tabHost.setCurrentTabByTag(HOME_TAB);
break;
case R.id.rd_btn_at:
tabHost.setCurrentTabByTag(AT_TAB);
break;
case R.id.rd_btn_message:
tabHost.setCurrentTabByTag(MSG_TAB);
break;
case R.id.rd_btn_search:
tabHost.setCurrentTabByTag(SEARCH_TAB);
break;
case R.id.rd_btn_menu:
tabHost.setCurrentTabByTag(MENU_TAB);
break;
default :
break;
}
}
});
}
}
这里是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost
android:id="@+id/main_tab_host"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:id="@+id/rg_main_btns"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:background="@color/ivory">
<RadioButton
android:id="@+id/rd_btn_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_tab"
style="@style/main_btn_style"
android:drawableTop="@drawable/main_page_btn"
/>
<RadioButton
android:id="@+id/rd_btn_at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/at_tab"
style="@style/main_btn_style"
android:drawableTop="@drawable/at_gage_btn"
/>
<RadioButton
android:id="@+id/rd_btn_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/msg_tab"
style="@style/main_btn_style"
android:drawableTop="@drawable/message_page_btn"
/>
<RadioButton
android:id="@+id/rd_btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search_tab"
style="@style/main_btn_style"
android:drawableTop="@drawable/search_page_btn"
/>
<RadioButton
android:id="@+id/rd_btn_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_tab"
style="@style/main_btn_style"
android:drawableTop="@drawable/menu_page_btn"
/>
</RadioGroup>
</FrameLayout>
</TabHost>
</LinearLayout>