1.佈局XML:android
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabhost"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/bottomtip" android:layout_width="match_parent" android:layout_height="50dp" android:text="@string/funPageBtn2" android:gravity="center_horizontal|center_vertical" android:layout_alignParentTop="true" android:textColor="@android:color/white" android:background="@android:color/black" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@android:id/tabs" android:layout_below="@+id/bottomtip" /> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@android:color/black" /> </RelativeLayout> </TabHost>
佈局效果理解:
app
2.Activity類實現佈局
package com.test.appdemo1.actlearn; import com.test.appdemo1.R; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; import android.widget.TabHost.TabSpec; @SuppressWarnings("deprecation") public class TabOptTopAct extends TabActivity { private TabHost tabHost; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabtoplayout); initTab(); } private void initTab() { tabHost = getTabHost(); TabSpec tabSpec = null; Intent intent = new Intent(this,MainActivity.class); //第一個 tabSpec = tabHost.newTabSpec("tab1") .setIndicator("首頁",getResources().getDrawable(R.drawable.home)) .setContent(intent); tabHost.addTab(tabSpec); //第二個 intent = new Intent(this,NextActivity.class); tabSpec = tabHost.newTabSpec("tab2") .setIndicator("郵件",getResources().getDrawable(R.drawable.mail)) .setContent(intent); tabHost.addTab(tabSpec); //第三個 intent = new Intent(this,ServiceActDemo.class); tabSpec = tabHost.newTabSpec("tab3") .setIndicator("音樂",getResources().getDrawable(R.drawable.music)) .setContent(intent); tabHost.addTab(tabSpec); //第四個 intent = new Intent(this,FrameLearnLayoutAct.class); tabSpec = tabHost.newTabSpec("tab4") .setIndicator("計算",getResources().getDrawable(R.drawable.calc)) .setContent(intent); tabHost.addTab(tabSpec); tabHost.setCurrentTab(1); } public void onBackPressed() { super.onBackPressed(); finish(); } }
最後說明:若是想把選項卡放到頂部只需修改佈局文件中RelativeLayout裏面的三個元素的位置this
如有問題請你們指正.spa
程序運行效果如上圖片所示.code