public class MainActivity extends TabActivity {
private TabHost tabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tabHost=this.getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent=new Intent().setClass(this, StartActivity.class);
spec=tabHost.newTabSpec("开始").setIndicator("开始").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this,PlanActivity.class);
spec=tabHost.newTabSpec("计划").setIndicator("计划").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, HistoryActivity.class);
spec=tabHost.newTabSpec("历史").setIndicator("历史").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, PersonalInformationActivity.class);
spec=tabHost.newTabSpec("我").setIndicator("我").setContent(intent);
tabHost.addTab(spec);
// 默认设置选中start页面
tabHost.setCurrentTab(0);
RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_sport:
tabHost.setCurrentTabByTag("开始");
break;
case R.id.main_plan:
tabHost.setCurrentTabByTag("计划");
break;
case R.id.main_history:
tabHost.setCurrentTabByTag("历史");
break;
case R.id.main_set:
tabHost.setCurrentTabByTag("我");
break;
}
}
});
}
@Override
protected void onStop(){
super.onStop();
}
@Override
protected void onDestroy(){
super.onDestroy();
}
}
layout文件如下:
<?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"
android:background="#ffffff" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:background="#ffffff"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:background="#ffffff"
android:visibility="gone" />
<FrameLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/main_tab_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:background="#ffffff"
>
<RadioButton
android:id="@+id/main_sport"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:checked="true"
android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
/>
<RadioButton
android:id="@+id/main_plan"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
/>
<RadioButton
android:id="@+id/main_history"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
/>
<RadioButton
android:id="@+id/main_set"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
/>
</RadioGroup>
</FrameLayout>
</LinearLayout>
</TabHost>
什么是TabHost?
Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page[1].
什么是TabSpec?
A tab has a tab indicator, content, and a tag that is used to keep track of it. This builder helps choose among these options. For the tab indicator, your choices are: 1) set a label 2) set a label and an icon For the tab content, your choices are: 1) the id of a View 2) a TabHost.TabContentFactory that creates the View content. 3) an Intent that launches an Activity[2].
注意:For apps developing against HONEYCOMB
or later, tabs are typically presented in the UI using the newActionBar.newTab()
and related APIs for placing tabs within their action bar area[2].