TabHost可以很方便地在窗口上放置多個標簽頁,每個標簽頁相當于獲得了一個外部容器相同大小的組件擺放區域
TabHost的主要組件是:
TabWiget:代表一個選項卡標簽條
TabSpec:代表選項卡的一個Tab頁
TabHost的基本用法:
?1,在界面布局中定義TabHost組件,并未改組件定義該選項卡的內容
?2,繼承TabActivity
?3,調用TabActivity的getTabHost()方法獲取TabHost對象(獲取)
?4,TabHost對象的addTab方法創建,添加選項卡(添加)
activity_main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<!--TabHost布局文件的結構:
1,TabHost容器必須包含TabWidget,FrameLayout
2,FrameLayout則用于“層疊”組合多個選項頁面,TabWidget定義選項卡的標題條,隨FrameLayout中的層疊組件均分
3,三個組件的ID有要求:
TabHost的ID必須是android:id="@android:id/tabhost"
TabWidget的ID必須是 android:id="@android:id/tabs"
FrameLayout的ID必須是 android:id="@android:id/tabcontent"
-->
<!-- 定義一個TabHost, ID必須是android提供的ID,android:id="@android:id/tabhost"-->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定義一個TabWiget選項卡標題條,ID必須是android提供的ID,android:id="@android:id/tabs" -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 定義一個幀布局FrameLayout,代表一個Tab頁面,ID必須是android提供的ID, android:id="@android:id/tabcontent" -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 當然可以放其他復雜的布局 -->
<LinearLayout
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第一個Tab頁"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第二個Tab頁"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第三個Tab頁"
android:textSize="20dp"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
~~~
TabHost布局文件的特點是:
TabHost布局文件的結構:
1,TabHost容器必須包含TabWidget,FrameLayout
2,FrameLayout則用于“層疊”組合多個選項頁面,TabWidget定義選項卡的標題條,隨FrameLayout中的層疊組件均分
3,三個組件的ID有要求:
TabHost的ID必須是android:id="@android:id/tabhost"
TabWidget的ID必須是 android:id="@android:id/tabs"
FrameLayout的ID必須是 android:id="@android:id/tabcontent"
MainActivity.java
~~~
package com.example.tabhosttest;
import android.app.Activity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {//繼承的是TabActivity
/*TabHost的基本用法:
* 1,在界面布局中定義TabHost組件,并未改組件定義該選項卡的內容
* 2,繼承TabActivity
* 3,調用TabActivity的getTabHost()方法獲取TabHost對象
* 4,TabHost對象的addTab方法創建,添加選項卡
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取該activity里面的TabHost組件
TabHost tabhost=getTabHost();
//創建第一個tab頁對象,TabSpec代表一個選項卡頁面,要設置標題和內容,內容是布局文件中FrameLayout中
TabSpec tab1=tabhost.newTabSpec("tab1");
tab1.setIndicator("已接電話");//設置標題
tab1.setContent(R.id.tab01);//設置內容
//添加tab頁
tabhost.addTab(tab1);
//創建第二個tab頁對象
TabSpec tab2=tabhost.newTabSpec("tab1");
tab2.setIndicator("已撥電話");//設置標題
tab2.setContent(R.id.tab02);//設置內容
//添加tab頁
tabhost.addTab(tab2);
//創建第三個tab頁對象
TabSpec tab3=tabhost.newTabSpec("tab1");
tab3.setIndicator("未接電話");//設置標題
tab3.setContent(R.id.tab03);//設置內容
//添加tab頁
tabhost.addTab(tab3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~



- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite