歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TabHost用法詳解

Android TabHost用法詳解

日期:2017/3/1 10:20:23   编辑:Linux編程

最近研究了一下Contacts源碼,仿照上面自己寫了一個TabHostTest程序,現整理如下:

main.xml布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <TabWidget android:id="@android:id/tabs"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. />
  14. <FrameLayout android:id="@android:id/tabcontent"
  15. android:layout_width="match_parent"
  16. android:layout_height="0dip"
  17. android:layout_weight="1"
  18. />
  19. </LinearLayout>
  20. </TabHost>

inner.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. s
  11. <FrameLayout android:id="@android:id/tabcontent"
  12. android:layout_width="fill_parent"
  13. android:layout_height="0dip"
  14. android:layout_weight="1"
  15. />
  16. <TabWidget android:id="@android:id/tabs"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. />
  20. </LinearLayout>
  21. </TabHost>

Main.java (主Activity類):

  1. package com.android.test;
  2. import android.app.Activity;
  3. import android.app.TabActivity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.provider.CallLog.Calls;
  7. import android.provider.Contacts.Intents.UI;
  8. import android.view.Window;
  9. import android.widget.TabHost;
  10. public class Main extends TabActivity implements TabHost.OnTabChangeListener {
  11. private static final int TAB_INDEX_DIALER = 0;
  12. private static final int TAB_INDEX_CALL_LOG = 1;
  13. private static final int TAB_INDEX_CONTACTS = 2;
  14. private static final int TAB_INDEX_FAVORITES = 3;
  15. private TabHost mTabHost;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. final Intent intent = getIntent();
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);
  21. setContentView(R.layout.main);
  22. mTabHost = getTabHost();
  23. mTabHost.setOnTabChangedListener(this);
  24. // Setup the tabs
  25. setupDialerTab();
  26. setupCallLogTab();
  27. setupContactsTab();
  28. setupFavoritesTab();
  29. setCurrentTab(intent);
  30. }
  31. public void onTabChanged(String tabId) {
  32. Activity activity = getLocalActivityManager().getActivity(tabId);
  33. if (activity != null) {
  34. activity.onWindowFocusChanged(true);
  35. }
  36. }
  37. private void setupCallLogTab() {
  38. // Force the class since overriding tab entries doesn't work
  39. Intent intent = new Intent("com.android.phone.action.RECENT_CALLS");
  40. intent.setClass(this, Inner.class);
  41. mTabHost.addTab(mTabHost.newTabSpec("call_log")
  42. .setIndicator("通話記錄",
  43. getResources().getDrawable(R.drawable.ic_tab_unselected_recent))
  44. .setContent(intent));
  45. }
  46. private void setupDialerTab() {
  47. Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
  48. intent.setClass(this, Inner.class);
  49. mTabHost.addTab(mTabHost.newTabSpec("dialer")
  50. .setIndicator("撥號",
  51. getResources().getDrawable(R.drawable.ic_tab_unselected_dialer))
  52. .setContent(intent));
  53. }
  54. private void setupContactsTab() {
  55. Intent intent = new Intent(UI.LIST_DEFAULT);
  56. intent.setClass(this, Main.class);
  57. mTabHost.addTab(mTabHost.newTabSpec("contacts")
  58. .setIndicator("通訊錄",
  59. getResources().getDrawable(R.drawable.ic_tab_unselected_contacts))
  60. .setContent(intent));
  61. }
  62. private void setupFavoritesTab() {
  63. Intent intent = new Intent(UI.LIST_STREQUENT_ACTION);
  64. intent.setClass(this, Inner.class);
  65. mTabHost.addTab(mTabHost.newTabSpec("favorites")
  66. .setIndicator("收藏",
  67. getResources().getDrawable(R.drawable.ic_tab_unselected_starred))
  68. .setContent(intent));
  69. }
  70. /**
  71. * Sets the current tab based on the intent's request type
  72. *
  73. * @param intent Intent that contains information about which tab should be selected
  74. */
  75. private void setCurrentTab(Intent intent) {
  76. // Dismiss menu provided by any children activities
  77. Activity activity = getLocalActivityManager().
  78. getActivity(mTabHost.getCurrentTabTag());
  79. if (activity != null) {
  80. activity.closeOptionsMenu();
  81. }
  82. // Tell the children activities that they should ignore any possible saved
  83. // state and instead reload their state from the parent's intent
  84. intent.putExtra("", true);
  85. // Choose the tab based on the inbound intent
  86. String componentName = intent.getComponent().getClassName();
  87. if (getClass().getName().equals(componentName)) {
  88. if (false) {
  89. //in a call, show the dialer tab(which allows going back to the call)
  90. mTabHost.setCurrentTab(TAB_INDEX_DIALER);
  91. } else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
  92. // launched from history (long-press home) --> nothing to change
  93. } else if (true) {
  94. // The dialer was explicitly requested
  95. mTabHost.setCurrentTab(TAB_INDEX_DIALER);
  96. }
  97. }
  98. }
  99. }

Inner.java類:

  1. package com.android.test;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.Window;
  6. import android.widget.TabHost;
  7. import android.widget.TabWidget;
  8. import android.widget.TextView;
  9. public class Inner extends TabActivity implements TabHost.OnTabChangeListener {
  10. private static final int TAB_INDEX_ALL = 0;
  11. private static final int TAB_INDEX_MISSED = 1;
  12. private static final int TAB_INDEX_OUTGOING = 2;
  13. private static final int TAB_INDEX_RECEIVED = 3;
  14. private TabHost mTabHost;
  15. private TabWidget mTabWidget;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. requestWindowFeature(Window.FEATURE_NO_TITLE);
  20. setContentView(R.layout.inner);
  21. mTabHost = getTabHost();
  22. mTabHost.setOnTabChangedListener(this);
  23. setupTabs();
  24. mTabWidget = mTabHost.getTabWidget();
  25. mTabWidget.setStripEnabled(false);
  26. for (int i = 0; i < mTabWidget.getChildCount(); i++) {
  27. TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
  28. android.R.id.title);
  29. tv.setTextColor(this.getResources().getColorStateList(
  30. android.R.color.white));
  31. tv.setPadding(0, 0, 0,(int) tv.getTextSize());
  32. tv.setText("Tab" + i);
  33. mTabWidget.getChildAt(i).getLayoutParams().height =(int ) (3* tv.getTextSize());
  34. mTabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
  35. }
  36. }
  37. public void onTabChanged(String tabId) {
  38. }
  39. private void setupTabs() {
  40. mTabHost.addTab(mTabHost.newTabSpec("all").setIndicator(
  41. getString(R.string.inner)).setContent(
  42. new Intent(this, Other.class)));
  43. mTabHost.addTab(mTabHost.newTabSpec("Missed").setIndicator(
  44. getString(R.string.inner)).setContent(
  45. new Intent(this, Other.class)));
  46. mTabHost.addTab(mTabHost.newTabSpec("Outgoing").setIndicator(
  47. getString(R.string.inner)).setContent(
  48. new Intent(this, Other.class)));
  49. mTabHost.addTab(mTabHost.newTabSpec("Received").setIndicator(
  50. getString(R.string.inner)).setContent(
  51. new Intent(this, Other.class)));
  52. }
  53. }

效果圖如下:


更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved