(TabWidget ,FrameLayout 在TabHost内。TabWidget 和FrameLayout 平行的关系)
1.MainActivity
public class MainActivity extends TabActivity {
private TabHost tabHost;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏
setContentView(R.layout.activity_main);
tabHost=getTabHost();
addTab("study",R.string.title_study,R.drawable.study,StudyActivity.class);
addTab("search",R.string.title_search,R.drawable.search,StudyActivity.class);
addTab("game",R.string.title_game,R.drawable.game,StudyActivity.class);
addTab("save",R.string.title_save,R.drawable.save,StudyActivity.class);
addTab("help",R.string.title_help,R.drawable.help,StudyActivity.class);
}
@SuppressWarnings("rawtypes")
private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){
tabHost.addTab(tabHost.newTabSpec(tag)
.setIndicator(getString(title_introduction),
getResources().getDrawable(title_icon))
.setContent(new Intent(this,ActivityClass)));
}
public boolean onCreateOptionsMenu(Menu menu){
//inflate the menu;this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>