Tab與TabHost
這就是Tab,而盛放Tab的容器就是TabHost
如何實現??
每一個Tab還對應了一個佈局,這個就有點好玩了。一個Activity,對應了多個功能佈局。
①新建一個Tab項目,注意,不要生成main Activity
這裏不要選
②在包裏面新建一個類MyTab,繼承於TabActivity
其實,TabActivity是Activity的子類
package zyf.tab.test;
import android.app.TabActivity;
public class MyTab extends TabActivity {
}
package zyf.tab.test;
import android.app.TabActivity;
import android.os.Bundle;
public class MyTab extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
<activity android:name=".MyTab">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<?xml version="1.0" encoding="utf-8"?>
<!-- 這裏是根節點佈局 -- >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 第一個Tab 對應的佈局 -- >
<LinearLayout android:id="@+id/widget_layout_Blue"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical" >
<EditText android:id="@+id/widget34" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="EditText"
android:textSize="18sp">
</EditText>
<Button android:id="@+id/widget30" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button">
</Button>
</LinearLayout>
<!-- 第二個Tab 對應的佈局 -- >
<LinearLayout android:id="@+id/widget_layout_red"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical" >
<AnalogClock android:id="@+id/widget36"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
</LinearLayout>
<!-- 第三個Tab 對應的佈局 -- >
<LinearLayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
androidrientation="vertical">
<RadioGroup android:id="@+id/widget43"
android:layout_width="166px" android:layout_height="98px"
androidrientation="vertical">
<RadioButton android:id="@+id/widget44"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
<RadioButton android:id="@+id/widget45"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
</RadioGroup>
</LinearLayout>
</FrameLayout>
private TabHost myTabhost;
myTabhost=this.getTabHost();//從TabActivity上面獲取放置Tab的TabHost
LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);
//from(this)從這個TabActivity獲取LayoutInflater
//R.layout.main 存放Tab佈局
//通過TabHost獲得存放Tab標籤頁內容的FrameLayout
//是否將inflate 拴繫到根佈局元素上
myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));
//設置一下TabHost的顏色
myTabhost
.addTab(myTabhost.newTabSpec("TT")// 製造一個新的標籤TT
.setIndicator("KK",
getResources().getDrawable(R.drawable.ajjc))
// 設置一下顯示的標題爲KK,設置一下標籤圖標爲ajjc
.setContent(R.id.widget_layout_red));
//設置一下該標籤頁的佈局內容爲R.id.widget_layout_red,這是FrameLayout中的一個子Layout
myTabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
}
});
先把在XML中設計好的MENU放到一個int數組裏
private static final int myMenuResources[] = { R.menu.phonebook_menu,
R.menu.addphone_menu, R.menu.chatting_menu, R.menu.userapp_menu };
@Override
public void onTabChanged(String tagString) {
// TODO Auto-generated method stub
if (tagString.equals("One")) {
myMenuSettingTag = 1;
}
if (tagString.equals("Two")) {
myMenuSettingTag = 2;
}
if (tagString.equals("Three")) {
myMenuSettingTag = 3;
}
if (tagString.equals("Four")) {
myMenuSettingTag = 4;
}
if (myMenu != null) {
onCreateOptionsMenu(myMenu);
}
}
然後onCreateOptionsMenu(Menu menu) 方法中通過MenuInflater過濾器動態加入MENU
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
// Hold on to this
myMenu = menu;
myMenu.clear();//清空MENU菜單
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
//從TabActivity這裏獲取一個MENU過濾器
switch (myMenuSettingTag) {
case 1:
inflater.inflate(myMenuResources[0], menu);
//動態加入數組中對應的XML MENU菜單
break;
case 2:
inflater.inflate(myMenuResources[1], menu);
break;
case 3:
inflater.inflate(myMenuResources[2], menu);
break;
case 4:
inflater.inflate(myMenuResources[3], menu);
break;
default:
break;
}
return super.onCreateOptionsMenu(menu);
}
⑩運行效果