一,创建三个Activity类
MainActiviey.java
package tianshuai.home_page;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends ActivityGroup implements View.OnClickListener
{
public static TabHost tab_host;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createTab();
}
public void createTab()
{
tab_host = (TabHost) findViewById(R.id.tab_host);
tab_host.setup(this.getLocalActivityManager());
tab_host.setOnClickListener(this);
TabSpec ts1 = tab_host.newTabSpec("手机酷站");
ts1.setIndicator("正在播放", getResources().getDrawable(R.drawable.find));
ts1.setContent(new Intent(this, cellphone_web.class));
tab_host.addTab(ts1);
TabSpec ts2 = tab_host.newTabSpec("软件游戏");
ts2.setIndicator("本地曲库", getResources().getDrawable(R.drawable.history));
ts2.setContent(new Intent(this, software.class));
tab_host.addTab(ts2);
tab_host.setCurrentTab(1);
}
public void onClick(View arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
二,cellphone_web.java 跟 software.java均为继承 Activity 的空类就可以
三,main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@+id/tab_host"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
</LinearLayout>
注意:TabWidget和FrameLayout 有不同的ID命名空间android:id="@android:id/idnames",这个是必须的因此TabHost才能自动找到它,Activity需要继承TabActivity。
如果想让选项卡在上方的话:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
内容想在上边,TabWidget 必须与 FrameLayout 换换位置