第一步:首先你得瞭解TabHost和TabWidget, 將TabHost分為兩個部分,一個是放內容的,還有一個就是放選項卡的,我們這裡選項卡用TabWidget。TabHost的id應該寫為
android:id="@android:id/tabhost",而不是以前的那個加號,這樣可以直接在Activity裡面通過getTabHost方法得到此TabHost對象。那如何才能將選項卡放在最底部呢,這其實很簡單,還記得 RelativeLayout中android:layout_alignBottom 這個不,也就是說將TabWidget和選項卡內容放到RelativeLayout布局裡,廢話不多說了,看代碼
<?xml version="1.0" encoding="utf-8"?>
- <TabHost android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@android:id/tabhost"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:padding="3dp"
- >
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@android:id/tabcontent"
- android:layout_weight="1"
- >
- </FrameLayout>
- <TabWidget
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:id="@android:id/tabs"
- android:layout_alignBottom = "@android:id/tabcontent"
- />
- </RelativeLayout>
- </TabHost>
以上xml代碼可以通用哈,FrameLayout裡用來放內容的,Tab的xml基本結構就這樣搭好了,
第二步:接下來就是往Activity裡面寫點東東了,這個Activity必須要繼承下TabActivity,要不然就算你setContentView了上面那個xml也沒法通過getTabHost()獲得布局中的tabHost。
然後通過getTabHost得到TabHost對象。我這裡打算建立四個選項卡,newTabSpec 這是為你的每一個選項卡上面打上一個Tag,也就是標記標記而已,特殊情況下你可以通過這個來尋找到你的選項卡。setIndicator就是在你的選項卡上設定一些內容,至於什麼內容,你懂的,要麼是文字,要麼是圖片,要麼就是文字加圖片,當然有時候覆雜的話還會寫上布局檔案。我這裡就放上一個圖片吧,然後這個圖片加上一些特效,懶得在xml寫什麼布局了,直接在代碼寫了,返回的是一個View對象,廢話有點多,直接看代碼吧
private class TabView extends LinearLayout {
- ImageView imageView ;
- public TabView(Context c, int drawable, int drawableselec) {
- super(c);
- imageView = new ImageView(c);
- StateListDrawable listDrawable = new StateListDrawable();
- listDrawable.addState(SELECTED_STATE_SET, this.getResources()
- .getDrawable(drawableselec));
- listDrawable.addState(ENABLED_STATE_SET, this.getResources()
- .getDrawable(drawable));
- imageView.setImageDrawable(listDrawable);
- imageView.setBackgroundColor(Color.TRANSPARENT);
- setGravity(Gravity.CENTER);
- addView(imageView);
- }
上面的代碼也就是自訂的一個View吧。
下面這個就是構建選項卡以及內容(選中的是第二個選項卡),我這裡就把每個不同的選項卡內容分別放在不同的Activity裡面,然後通過Tab來將這些Activity合并在一起,這樣顯得比較有層次感。
@Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main_tab);
- TabHost tabHost=getTabHost();
-
- TabView view = null;
-
- // 最近連絡人
- view = new TabView(this, R.drawable.bg_tab_dial_normal, R.drawable.bg_tab_dial_normal);
- view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));
-
- TabSpec recentContactSpec=tabHost.newTabSpec("RecentContact");
- recentContactSpec.setIndicator(view);
- Intent recentContactIntent = new Intent(this, RecentContactActivity.class);
- recentContactSpec.setContent(recentContactIntent);
- // 連絡人
- view = new TabView(this, R.drawable.bg_tab_contact_normal, R.drawable.bg_tab_contact_normal);
- view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));
-
- TabSpec contactBookSpec=tabHost.newTabSpec("ContactBook");
- contactBookSpec.setIndicator(view);
- Intent contactBookIntent = new Intent(this,ContactBookActivity.class);
- contactBookSpec.setContent(contactBookIntent);
-
- // 簡訊
- view = new TabView(this, R.drawable.bg_tab_sms_normal, R.drawable.bg_tab_sms_normal);
- view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));
-
- TabSpec smsMessageSpec = tabHost.newTabSpec("SmsMessage");
- smsMessageSpec.setIndicator(view);
- Intent smsMessageIntent = new Intent(this, SmsMessageActivity.class);
- smsMessageSpec.setContent(smsMessageIntent);
-
- //設定
- view = new TabView(this, R.drawable.bg_tab_setting_normal, R.drawable.bg_tab_setting_normal);
- view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));
-
- TabSpec settingSpec = tabHost.newTabSpec("Setting");
- settingSpec.setIndicator(view);
- Intent settingIntent = new Intent(this, SettingActivity.class);
- settingSpec.setContent(settingIntent);
-
- tabHost.addTab(recentContactSpec);
- tabHost.addTab(contactBookSpec);
- tabHost.addTab(smsMessageSpec);
- tabHost.addTab(settingSpec);
-
- tabHost.setCurrentTab(1);
- }
-
這個我沒有寫Demo,也就不提供詳細的Demo了,不過還是截個圖吧