TabHost选项卡的 功能和用法
TabHost可以很方便地在窗口上放置多個(gè)標(biāo)簽頁,每個(gè)標(biāo)簽頁相當(dāng)于獲得了一個(gè)外部容器相同大小的組件擺放區(qū)域
TabHost的主要組件是:
TabWiget:代表一個(gè)選項(xiàng)卡標(biāo)簽條
TabSpec:代表選項(xiàng)卡的一個(gè)Tab頁
TabHost的基本用法:
?1,在界面布局中定義TabHost組件,并未改組件定義該選項(xiàng)卡的內(nèi)容
?2,繼承TabActivity
?3,調(diào)用TabActivity的getTabHost()方法獲取TabHost對象(獲取)
?4,TabHost對象的addTab方法創(chuàng)建,添加選項(xiàng)卡(添加)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><!--TabHost布局文件的結(jié)構(gòu): 1,TabHost容器必須包含TabWidget,FrameLayout2,FrameLayout則用于“層疊”組合多個(gè)選項(xiàng)頁面,TabWidget定義選項(xiàng)卡的標(biāo)題條,隨FrameLayout中的層疊組件均分3,三個(gè)組件的ID有要求:TabHost的ID必須是android:id="@android:id/tabhost"TabWidget的ID必須是 android:id="@android:id/tabs"FrameLayout的ID必須是 android:id="@android:id/tabcontent"--> <!-- 定義一個(gè)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"><!-- 定義一個(gè)TabWiget選項(xiàng)卡標(biāo)題條,ID必須是android提供的ID,android:id="@android:id/tabs" --><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content" /><!-- 定義一個(gè)幀布局FrameLayout,代表一個(gè)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"><!-- 當(dāng)然可以放其他復(fù)雜的布局 --><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="第一個(gè)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="第二個(gè)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="第三個(gè)Tab頁"android:textSize="20dp"/></LinearLayout> </FrameLayout> </LinearLayout></TabHost>TabHost布局文件的特點(diǎn)是:
TabHost布局文件的結(jié)構(gòu):
1,TabHost容器必須包含TabWidget,FrameLayout
2,FrameLayout則用于“層疊”組合多個(gè)選項(xiàng)頁面,TabWidget定義選項(xiàng)卡的標(biāo)題條,隨FrameLayout中的層疊組件均分
3,三個(gè)組件的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組件,并未改組件定義該選項(xiàng)卡的內(nèi)容* 2,繼承TabActivity* 3,調(diào)用TabActivity的getTabHost()方法獲取TabHost對象* 4,TabHost對象的addTab方法創(chuàng)建,添加選項(xiàng)卡* */@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//獲取該activity里面的TabHost組件TabHost tabhost=getTabHost();//創(chuàng)建第一個(gè)tab頁對象,TabSpec代表一個(gè)選項(xiàng)卡頁面,要設(shè)置標(biāo)題和內(nèi)容,內(nèi)容是布局文件中FrameLayout中TabSpec tab1=tabhost.newTabSpec("tab1");tab1.setIndicator("已接電話");//設(shè)置標(biāo)題tab1.setContent(R.id.tab01);//設(shè)置內(nèi)容//添加tab頁tabhost.addTab(tab1);//創(chuàng)建第二個(gè)tab頁對象TabSpec tab2=tabhost.newTabSpec("tab1");tab2.setIndicator("已撥電話");//設(shè)置標(biāo)題tab2.setContent(R.id.tab02);//設(shè)置內(nèi)容//添加tab頁tabhost.addTab(tab2);//創(chuàng)建第三個(gè)tab頁對象TabSpec tab3=tabhost.newTabSpec("tab1");tab3.setIndicator("未接電話");//設(shè)置標(biāo)題tab3.setContent(R.id.tab03);//設(shè)置內(nèi)容//添加tab頁tabhost.addTab(tab3);}@Overridepublic 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;}@Overridepublic 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);} }
總結(jié)
以上是生活随笔為你收集整理的TabHost选项卡的 功能和用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Toast的功能和用法
- 下一篇: AlertDialog创建6种对话框的用