</pre><p>下面是用TabHost组件实现一个带底部导航栏的界面。</p><p></p><pre name="code" class="java">package com.Jim.tabhostdemo;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity implements OnTabChangeListener {
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化tabhost
tabHost = getTabHost();
TabSpec tabSpec1 = tabHost.newTabSpec("tag1")
//设置tabwidget的显示内容,此处还可以加入自定义view等
.setIndicator("游戏")
//设置tabwidget对应页面的显示内容
.setContent(new Intent(MainActivity.this, FirstActivity.class));
//添加第一个页面到tabhost中
tabHost.addTab(tabSpec1);
TabSpec tabSpec2 = tabHost.newTabSpec("tag2")
.setIndicator("音乐")
.setContent(new Intent(MainActivity.this, SecondActivity.class));
tabHost.addTab(tabSpec2);
TabSpec tabSpec3 = tabHost.newTabSpec("tag3")
.setIndicator("体育")
.setContent(new Intent(MainActivity.this, ThirdActivity.class));
tabHost.addTab(tabSpec3);
// 初始化tabwidget
updateTab(tabHost);
// 设置事件监听
tabHost.setOnTabChangedListener(this);
}
private void updateTab(TabHost tabHost) {
for (int i = 0; i < 3; i++) {
View view = tabHost.getTabWidget().getChildAt(i);
if (tabHost.getCurrentTab() == i) {
//设置tabwidget选中时的背景颜色
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red));
} else {
//设置未选中的背景颜色
view.setBackgroundDrawable(getResources().getDrawable(
R.drawable.grary));
}
}
}
@Override
public void onTabChanged(String tabId) {
// tabwidget的事件监听
updateTab(tabHost);
}
}
主XML文件如下:
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
效果如下: