国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之TabActivity用法實例詳解

2020-01-02 07:02:30
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發之TabActivity用法。分享給大家供大家參考,具體如下:

一.簡介

TabActivity繼承自Activity,目的是讓同一界面容納更多的內容。TabActivity實現標簽頁的功能,通過導航欄對各個頁面進行管理。

二.XML布局文件

注意:

1.TabActivity的布局文件要求以TabHost作為XML布局文件的根。

2.通常我們采用線性布局,所以<TabHost> 的子元素是 <LinearLayout>。

3.<TabWidget>對應Tab
<FrameLayout>則用于包含Tab需要展示的內容
需要注意的是<TabWidget> 和<FrameLayout>的Id 必須使用系統id,分別為android:id/tabs 和 android:id/tabcontent 。
因為系統會使用者兩個id來初始化TabHost的兩個實例變量(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的一些函數

(1)獲取

TabHost tabHost=this.getTabHost();

(2) 創建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)  設置當前的Tab (by index)

public void setCurrentTab (int index)

(6) 設置當前的(Tab by tag)

public void setCurrentTabByTag (String tag)

(7)設置TabChanged事件的響應處理

public void setOnTabChangedListener (TabHost.OnTabChangeListener l)

3.TabHost.TabSpec要設置tab的label和content,需要設置TabHost.TabSpec類。TabHost.TabSpec管理:

public String getTag ()public TabHost.TabSpec setContentpublic TabHost.TabSpec setIndicator

(1)Indicator這里的Indicator 就是Tab上的label,它可以

設置label :

setIndicator (CharSequence label)

設置label和icon :

setIndicator (CharSequence label, Drawable icon)

指定某個view :

setIndicator (View view)

(2)Content對于Content ,就是Tab里面的內容,可以

設置View的id :

setContent(int viewId)

用new Intent 來引入其他Activity的內容: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("藝術家").setIndicator("藝術家",             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);  }}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石楼县| 紫金县| 盐源县| 乌拉特后旗| 收藏| 永泰县| 黑龙江省| 安西县| 积石山| 盐边县| 盐城市| 文登市| 五莲县| 格尔木市| 夏河县| 定陶县| 遵化市| 汤阴县| 曲周县| 舒城县| 新龙县| 金溪县| 广南县| 内丘县| 新郑市| 苏州市| 娄底市| 益阳市| 三江| 辽阳县| 五河县| 中阳县| 河东区| 凤冈县| 浮梁县| 太原市| 永修县| 周宁县| 临安市| 樟树市| 临安市|