布局文件activity_main.xml
<?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="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"></FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_purple"></TabWidget>
</RelativeLayout>
</TabHost>
在MainActivity中使用
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost1 = getTabHost();
Intent intent1 = new Intent(MainActivity.this, MessageActivity.class);
TabHost.TabSpec spec1 = tabHost1.newTabSpec("消息");
Resources resources1 = getResources();
spec1.setIndicator("消息", resources1.getDrawable(R.drawable.koala));
spec1.setContent(intent1);
tabHost1.addTab(spec1);
TabHost tabHost2 = getTabHost();
Intent intent2 = new Intent(MainActivity.this, ContactActivity.class);
TabHost.TabSpec spec2 = tabHost2.newTabSpec("联系人");
Resources resources2 = getResources();
spec2.setIndicator("联系", resources2.getDrawable(R.drawable.contact));
spec2.setContent(intent2);
tabHost2.addTab(spec2);
TabHost tabHost3 = getTabHost();
Intent intent3 = new Intent(MainActivity.this, TrendsActivity.class);
TabHost.TabSpec spec3 = tabHost3.newTabSpec("动态");
Resources resources3 = getResources();
spec3.setIndicator("动态", resources3.getDrawable(R.drawable.feather));
spec3.setContent(intent3);
tabHost3.addTab(spec3);
}
}