1.直接继承TabActivity,函数getTabHost拿到TabActivity里面的TabHost对象
package com.example.tabtest;
import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost host = getTabHost();
//from(this)从这个TabActivity获取LayoutInflater
LayoutInflater.from(this).inflate(
R.layout.host, //host.xml存放Tab布局
host.getTabContentView(), //通过TabHost获取存放Tab标签页内容的FrameLayout
true);
host.setBackgroundColor(Color.WHITE);
host.addTab(host.newTabSpec("One")// make a new Tab
.setIndicator("", getResources().getDrawable(R.drawable.gimp))
// set the Title and Icon
.setContent(R.id.blue));
host.addTab(host.newTabSpec("Two")// make a new Tab
.setIndicator("", getResources().getDrawable(R.drawable.mumule))
// set the Title and Icon
.setContent(R.id.red));
host.addTab(host.newTabSpec("Three")// make a new Tab
.setIndicator("", getResources().getDrawable(R.drawable.notepad))
// set the Title and Icon
.setContent(R.id.green));
//setContentView(R.layout.activity_main);
}
@Override
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.如果不继承TabActivity,则TabHost需在xml文件中定义
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabHost必须包含一个 TabWidget和一个FrameLayout--> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabWidget的id属性必须为 @android:id/tabs--> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <!-- FrameLayout的id属性必须为 @android:id/tabcontent--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
package com.example.tabtest2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost host = (TabHost)findViewById(R.id.tabhost);
//没有继承TabActivity时,通过该种方法加载启动tabHost
host.setup();
host.addTab(host.newTabSpec("tab1")
.setIndicator("",
getResources().getDrawable(R.drawable.win))
.setContent(R.id.view1));
host.addTab(host.newTabSpec("tab3")
.setIndicator("", getResources().getDrawable(R.drawable.draw))
.setContent(R.id.view3));
host.addTab(host.newTabSpec("tab2")
.setIndicator("", getResources().getDrawable(R.drawable.lose))
.setContent(R.id.view2));
}
@Override
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;
}
}