1.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/content1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="内容1" />
<TextView
android:id="@+id/content2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="内容2" />
<TextView
android:id="@+id/content3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="内容3" />
</LinearLayout
2.MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 第二步:获取选项卡组
TabHost tabHost = getTabHost();
// 第三步:引用布局文件
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.activity_main, tabHost.getTabContentView());
Resources resources = getResources();
// 第四步:创建一个Tab对象
TabHost.TabSpec tab01 = tabHost.newTabSpec("tab01")
.setIndicator("选项1", resources.getDrawable(R.drawable.p1))
.setContent(R.id.content1);
TabHost.TabSpec tab02 = tabHost.newTabSpec("tab02")
.setIndicator("选项2", resources.getDrawable(R.drawable.p2))
.setContent(R.id.content2);
TabHost.TabSpec tab03 = tabHost.newTabSpec("tab03")
.setIndicator("选项3", resources.getDrawable(R.drawable.p3))
.setContent(R.id.content3);
// 第五步:将创建好的Tab对象放入到tabHost中
tabHost.addTab(tab01);
tabHost.addTab(tab02);
tabHost.addTab(tab03);
tabHost.setCurrentTab(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}