一,建立三個Activity類
MainActiviey.java
package tianshuai.home_page;</p><p>import android.app.Activity;<br />import android.app.ActivityGroup;<br />import android.content.Intent;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.TabHost;<br />import android.widget.TabHost.TabSpec;</p><p>public class MainActivity extends ActivityGroup implements View.OnClickListener<br />{<br /> public static TabHost tab_host;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState)<br /> {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> createTab();<br /> }</p><p> public void createTab()<br /> {<br /> tab_host = (TabHost) findViewById(R.id.tab_host);<br /> tab_host.setup(this.getLocalActivityManager());</p><p> tab_host.setOnClickListener(this);</p><p> TabSpec ts1 = tab_host.newTabSpec("手機酷站");<br /> ts1.setIndicator("現正播放", getResources().getDrawable(R.drawable.find));<br /> ts1.setContent(new Intent(this, cellphone_web.class));</p><p> tab_host.addTab(ts1);</p><p> TabSpec ts2 = tab_host.newTabSpec("軟體遊戲");<br /> ts2.setIndicator("本地曲庫", getResources().getDrawable(R.drawable.history));<br /> ts2.setContent(new Intent(this, software.class));</p><p> tab_host.addTab(ts2);<br /> tab_host.setCurrentTab(1);<br /> }</p><p> public void onClick(View arg0) {<br /> throw new UnsupportedOperationException("Not supported yet.");<br /> }</p><p>}
二,cellphone_web.java 跟 software.java均為繼承 Activity 的空類就可以
三,main.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"></p><p> <TabHost<br /> android:id="@+id/tab_host"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:padding="5dp"><br /> <FrameLayout<br /> android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:padding="5dp"<br /> android:layout_weight="1" /><br /> <TabWidget<br /> android:id="@android:id/tabs"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="0" /><br /> </LinearLayout><br /> </TabHost><br /></LinearLayout><br />
注意:TabWidget和FrameLayout 有不同的ID命名空間android:id="@android:id/idnames",這個是必須的因此TabHost才能自動找到它,Activity需要繼承TabActivity。
如果想讓選項卡在上方的話:
<FrameLayout<br /> android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:padding="5dp"<br /> android:layout_weight="0" /><br /> <TabWidget<br /> android:id="@android:id/tabs"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1" />內容想在上邊,TabWidget 必須與 FrameLayout 換換位置