1. 程式人生 > >Android TabActivity用法

Android TabActivity用法

tor icon sans post list star per -h text

TabActivity

首先Android裏面有個名為TabActivity來給我們方便使用。當中有下面能夠關註的函數: public TabHost getTabHost () 獲得當前TabActivity的TabHost public TabWidget getTabWidget () 獲得當前TabActivity 的TabWidget public void setDefaultTab (String tag) 這兩個函數非常易懂, 就是設置默認的Tab public void setDefaultTab (int index) 通過tab名——tag或者index(從0開始) protected void onRestoreInstanceState (Bundle state)
這 兩個函數的介紹能夠 protected void onSaveInstanceState (Bundle outState) 參考 Activity的生命周期

TabHost

那麽我們要用到的Tab載體是TabHost,須要從TabActivity.getTabHost獲取。 如今看看TabHost類。它有3個內嵌類:1個類TabHost.TabSpec,2個接口 TabHost.TabContentFactory和TabHost.OnTabChangeListener。後面會介紹這些類和接口。 TabHost類的一些函數: public void addTab (TabHost.TabSpec tabSpec)
加入 tab。參數TabHost.TabSpec通過以下的函數返回得到 public TabHost.TabSpec newTabSpec (String tag) 創 建TabHost.TabSpec public void clearAllTabs () remove全部的Tabs public int getCurrentTab () public String getCurrentTabTag () public View getCurrentTabView () public View getCurrentView () public FrameLayout getTabContentView ()
返回Tab content的FrameLayout public TabWidget getTabWidget () public void setCurrentTab (int index) 設置當前的Tab by index public void setCurrentTabByTag (String tag) 設置當前的Tab by tag public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 設置TabChanged事件的響應處理 public void setup () 這個函數後面介紹

TabHost.TabSpec

從上面的函數能夠知道怎樣加入tab了。要註意,這裏的Tag(標簽)。不是Tabbutton上的文字。

而要設置tab的label和content,須要設置TabHost.TabSpec類。

引用SDK裏面的話——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理這3個東西:

public String getTag () public TabHost.TabSpec setContent public TabHost.TabSpec setIndicator 我理解這裏的Indicator 就是Tab上的label,它能夠 設置labelsetIndicator (CharSequence label) 或者同一時候設置label和iconsetIndicator (CharSequence label, Drawable icon) 或者直接指定某個viewsetIndicator (View view) 對於Content 。就是Tab裏面的內容,能夠 設置View的idsetContent(int viewId) 或者TabHost.TabContentFactory 的createTabContent(String tag)來處理:setContent(TabHost.TabContentFactory contentFactory) 或者用new Intent 來引入其它Activity的內容:setContent(Intent intent)

主程序代碼
Acitvit裏面的代碼代碼 技術分享
  1. package com.yang.tabletest;
  2. import android.app.TabActivity;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.widget.TabHost;
  6. public class TableTestAcitivity extends TabActivity{
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. //setContentView(R.layout.main);
  12. //獲得當前TabActivity的TabHost
  13. TabHost tabHost = getTabHost();
  14. LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
  15. tabHost.addTab(tabHost.newTabSpec("tab1")
  16. .setIndicator("主頁")
  17. .setContent(R.id.view1));
  18. tabHost.addTab(tabHost.newTabSpec("tab2")
  19. .setIndicator("標題")
  20. .setContent(R.id.view2));
  21. tabHost.addTab(tabHost.newTabSpec("tab3")
  22. .setIndicator("簡單介紹")
  23. .setContent(R.id.view3));
  24. tabHost.addTab(tabHost.newTabSpec("tab4")
  25. .setIndicator("關於")
  26. .setContent(R.id.view4));
  27. }
  28. }


tabls.xml裏面的代碼 Tabi.xml代碼 技術分享
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent">
  4. <TextView android:id="@+id/view1"
  5. android:background="@drawable/blue"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:text="@string/tabs_1_tab_1"/>
  9. <TextView android:id="@+id/view2"
  10. android:background="@drawable/red"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:text="@string/tabs_1_tab_2"/>
  14. <TextView android:id="@+id/view3"
  15. android:background="@drawable/green"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:text="@string/tabs_1_tab_3"/>
  19. <TextView android:id="@+id/view4"
  20. android:background="@drawable/green"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent"
  23. android:text="@string/tabs_1_tab_4"/>
  24. </FrameLayout>
string.xml的代碼
Java代碼 技術分享
  1. <?xml version="1.0" encoding="utf-8"?

    >

  2. <resources>
  3. <string name="hello">Hello World, TableTestAcitivity!</string>
  4. <string name="app_name">阿福學習</string>
  5. <string name="tabs_1_tab_1">主頁</string>
  6. <string name="tabs_1_tab_2">標題</string>
  7. <string name="tabs_1_tab_3">關於</string>
  8. <string name="tabs_1_tab_4">返回</string>
  9. </resources>
color.xml代碼 Java代碼 技術分享
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <drawable name="darkgray">#404040ff</drawable>
  4. <drawable name="red">#ff00ff</drawable>
  5. <drawable name="green">#0ff0ff</drawable>
  6. <drawable name="lightgray">#c0c0c0ff</drawable>
  7. <drawable name="yellow">#ffFF33ff</drawable>
  8. <drawable name="blue">#00ffff</drawable>
  9. <drawable name="gray">#808080ff</drawable>
  10. <drawable name="magenta">#ff6699ff</drawable>
  11. <drawable name="cyan">#66ffffff</drawable>
  12. <drawable name="black">#000000</drawable>
  13. <drawable name="white">#FFFFFF</drawable>
  14. </resources>
第二個樣例的Activity代碼 Java代碼 技術分享
  1. package com.yang.tabletest;
  2. import android.app.TabActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.TabHost;
  6. import android.widget.TextView;
  7. public class TableTestAcitivity extends TabActivity implements TabHost.TabContentFactory{
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. final TabHost tabHost = getTabHost();
  13. tabHost.addTab(tabHost.newTabSpec("tab1")
  14. .setIndicator("主頁", getResources().getDrawable(R.drawable.test))
  15. .setContent(this));
  16. tabHost.addTab(tabHost.newTabSpec("tab2")
  17. .setIndicator("標題",getResources().getDrawable(R.drawable.test))
  18. .setContent(this));
  19. tabHost.addTab(tabHost.newTabSpec("tab3")
  20. .setIndicator("關於",getResources().getDrawable(R.drawable.test))
  21. .setContent(this));
  22. }
  23. @Override
  24. public View createTabContent(String arg0) {
  25. final TextView tv = new TextView(this);
  26. tv.setText("Content for tab with tag " + arg0);
  27. return tv;
  28. }
  29. }
  • 技術分享
  • 大小: 14.3 KB
  • TableTest.zip (25.4 KB)
  • 下載次數: 563
  • 技術分享
  • 大小: 12.6 KB
  • TableTest2.zip (29.8 KB)
  • 下載次數: 487

Android TabActivity用法