Android開發(fā)之TabActivity用法實(shí)例詳解
本文實(shí)例講述了Android開發(fā)之TabActivity用法。分享給大家供大家參考,具體如下:
一.簡(jiǎn)介
TabActivity繼承自Activity,目的是讓同一界面容納更多的內(nèi)容。TabActivity實(shí)現(xiàn)標(biāo)簽頁(yè)的功能,通過導(dǎo)航欄對(duì)各個(gè)頁(yè)面進(jìn)行管理。

二.XML布局文件
注意:
1.TabActivity的布局文件要求以TabHost作為XML布局文件的根。
2.通常我們采用線性布局,所以<TabHost> 的子元素是 <LinearLayout>。
3.<TabWidget>對(duì)應(yīng)Tab
<FrameLayout>則用于包含Tab需要展示的內(nèi)容
需要注意的是<TabWidget> 和<FrameLayout>的Id 必須使用系統(tǒng)id,分別為android:id/tabs 和 android:id/tabcontent 。
因?yàn)橄到y(tǒng)會(huì)使用者兩個(gè)id來初始化TabHost的兩個(gè)實(shí)例變量(mTabWidget 和 mTabContent)。
4.代碼示例
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
三.TabActivity
1.TabHost:TabHost是Tab的載體,用來管理Tab。
2.TabHost的一些函數(shù)
(1)獲取
TabHost tabHost=this.getTabHost();
(2) 創(chuàng)建TabHost.TabSpec
public TabHost.TabSpec newTabSpec (String tag)
(3)添加tab
public void addTab (TabHost.TabSpec tabSpec)
(4)remove所有的Tabs
public void clearAllTabs () public int getCurrentTab ()
(5) 設(shè)置當(dāng)前的Tab (by index)
public void setCurrentTab (int index)
(6) 設(shè)置當(dāng)前的(Tab by tag)
public void setCurrentTabByTag (String tag)
(7)設(shè)置TabChanged事件的響應(yīng)處理
public void setOnTabChangedListener (TabHost.OnTabChangeListener l)
3.TabHost.TabSpec要設(shè)置tab的label和content,需要設(shè)置TabHost.TabSpec類。TabHost.TabSpec管理:
public String getTag () public TabHost.TabSpec setContent public TabHost.TabSpec setIndicator
(1)Indicator這里的Indicator 就是Tab上的label,它可以
設(shè)置label :
setIndicator (CharSequence label)
設(shè)置label和icon :
setIndicator (CharSequence label, Drawable icon)
指定某個(gè)view :
setIndicator (View view)
(2)Content對(duì)于Content ,就是Tab里面的內(nèi)容,可以
設(shè)置View的id :
setContent(int viewId)
用new Intent 來引入其他Activity的內(nèi)容:setContent(Intent intent)
package com.zhanglong.music;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TabHost;
public class MainActivity extends TabActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, ListActivity.class);
spec = tabHost.newTabSpec("音樂").setIndicator("音樂",
res.getDrawable(R.drawable.item))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("藝術(shù)家").setIndicator("藝術(shù)家",
res.getDrawable(R.drawable.artist))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("專輯").setIndicator("專輯",
res.getDrawable(R.drawable.album))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放",
res.getDrawable(R.drawable.album))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android線程管理之ActivityThread
- Android Activity 橫豎屏切換的生命周期
- 詳解Android Activity之間切換傳遞數(shù)據(jù)的方法
- Android動(dòng)態(tài)加載Activity原理詳解
- Android Activity回收與操作超時(shí)處理
- Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解
- 詳解Android開發(fā)中Activity的四種launchMode
- 全面解析Android應(yīng)用開發(fā)中Activity類的用法
- Android 暫停和恢復(fù)Activity
- Android中子線程和UI線程通信詳解
- android中UI主線程與子線程深入分析
- android開發(fā)教程之子線程中更新界面
- android使用handler ui線程和子線程通訊更新ui示例
- Android實(shí)現(xiàn)在子線程中更新Activity中UI的方法
相關(guān)文章
Kotlin遍歷集合導(dǎo)致并發(fā)修改異常的原因和解決方法
這篇文章主要介紹了Kotlin遍歷集合導(dǎo)致并發(fā)修改異常的原因和解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例
這篇文章主要介紹了Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
android開發(fā)實(shí)現(xiàn)列表控件滾動(dòng)位置精確保存和恢復(fù)的方法(推薦)
下面小編就為大家?guī)硪黄猘ndroid開發(fā)實(shí)現(xiàn)列表控件滾動(dòng)位置精確保存和恢復(fù)的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
淺談Android實(shí)踐之ScrollView中滑動(dòng)沖突處理解決方案
涉及到了ViewPager,MapView,ListView,就需要ScrollView來做一下支援,這篇文章主要介紹了淺談Android實(shí)踐之ScrollView中滑動(dòng)沖突處理解決方案,有需要的可以來了解一下。2016-12-12
Android UI系列-----ScrollView和HorizontalScrollView的詳解
本篇文章主要是介紹的Android UI系列-----ScrollView和HorizontalScrollView,ScrollView和HorizontalScrollView都是布局容器,有需要的可以了解一下。2016-11-11
Android 軟鍵盤彈出時(shí)把原來布局頂上去的解決方法
本文主要介紹了Android軟鍵盤彈出時(shí)把原來布局頂上去的解決方法。具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01
android monkey自動(dòng)化測(cè)試改為java調(diào)用monkeyrunner Api
一般情況下我們使用android中的monkeyrunner進(jìn)行自動(dòng)化測(cè)試時(shí),使用的是python語(yǔ)言來寫測(cè)試腳本。不過,最近發(fā)現(xiàn)可以用java調(diào)用monkeyrunner Api,于是,就簡(jiǎn)單研究了一下。這里做一些總結(jié)。希望有對(duì)在研究的午飯可以有所用處2012-11-11

