Android TabHost用法详解


最近研究了一下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.   
  7.     <LinearLayout  
  8.         android:orientation="vertical"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent">  
  11.   
  12.         <TabWidget android:id="@android:id/tabs"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content"  
  15.         />  
  16.   
  17.         <FrameLayout android:id="@android:id/tabcontent"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="0dip"  
  20.             android:layout_weight="1"  
  21.         />  
  22.     </LinearLayout>  
  23. </TabHost>  

inner.xml文件:

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

Main.java (主Activity类):

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

Inner.java类:

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

效果图如下:


更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容