欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TabHost组件使用方法详解

程序员文章站 2024-03-03 17:10:16
最近研究了一下contacts源码,仿照上面自己写了一个tabhosttest程序,现整理如下: main.xml布局文件:

最近研究了一下contacts源码,仿照上面自己写了一个tabhosttest程序,现整理如下:

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<tabhost xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@android:id/tabhost" 
 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" 
  /> 
 
  <framelayout android:id="@android:id/tabcontent" 
   android:layout_width="match_parent" 
   android:layout_height="0dip" 
   android:layout_weight="1" 
  /> 
 </linearlayout> 
</tabhost> 

inner.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
 
<tabhost xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@android:id/tabhost" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"> 
 
 <linearlayout 
  android:orientation="vertical" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <framelayout android:id="@android:id/tabcontent" 
   android:layout_width="fill_parent" 
   android:layout_height="0dip" 
   android:layout_weight="1" 
  /> 
   
  <tabwidget android:id="@android:id/tabs" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
  /> 
 
 
 </linearlayout> 
</tabhost>

  main.java (主activity类):

package com.android.test; 
 
import android.app.activity; 
import android.app.tabactivity; 
import android.content.intent; 
import android.os.bundle; 
import android.provider.calllog.calls; 
import android.provider.contacts.intents.ui; 
import android.view.window; 
import android.widget.tabhost; 
 
public class main extends tabactivity implements tabhost.ontabchangelistener { 
 private static final int tab_index_dialer = 0; 
 private static final int tab_index_call_log = 1; 
 private static final int tab_index_contacts = 2; 
 private static final int tab_index_favorites = 3; 
 
 private tabhost mtabhost; 
  
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
 
  final intent intent = getintent(); 
 
  requestwindowfeature(window.feature_no_title); 
   
  setcontentview(r.layout.main); 
 
  mtabhost = gettabhost(); 
  mtabhost.setontabchangedlistener(this); 
 
  // setup the tabs 
  setupdialertab(); 
  setupcalllogtab(); 
  setupcontactstab(); 
  setupfavoritestab(); 
 
  setcurrenttab(intent); 
 } 
 
 public void ontabchanged(string tabid) { 
   activity activity = getlocalactivitymanager().getactivity(tabid); 
   if (activity != null) { 
    activity.onwindowfocuschanged(true); 
   } 
 } 
  private void setupcalllogtab() { 
   // force the class since overriding tab entries doesn't work 
   intent intent = new intent("com.android.phone.action.recent_calls"); 
 
   intent.setclass(this, inner.class); 
   mtabhost.addtab(mtabhost.newtabspec("call_log") 
     .setindicator("通话记录", 
       getresources().getdrawable(r.drawable.ic_tab_unselected_recent)) 
     .setcontent(intent)); 
  } 
  
 private void setupdialertab() { 
  intent intent = new intent("com.android.phone.action.touch_dialer"); 
  intent.setclass(this, inner.class); 
 
  mtabhost.addtab(mtabhost.newtabspec("dialer") 
    .setindicator("拨号", 
      getresources().getdrawable(r.drawable.ic_tab_unselected_dialer)) 
    .setcontent(intent)); 
 } 
 
 private void setupcontactstab() { 
  intent intent = new intent(ui.list_default); 
  intent.setclass(this, main.class); 
 
  mtabhost.addtab(mtabhost.newtabspec("contacts") 
    .setindicator("通讯录", 
      getresources().getdrawable(r.drawable.ic_tab_unselected_contacts)) 
    .setcontent(intent)); 
 } 
 
 private void setupfavoritestab() { 
  intent intent = new intent(ui.list_strequent_action); 
  intent.setclass(this, inner.class); 
 
  mtabhost.addtab(mtabhost.newtabspec("favorites") 
    .setindicator("收藏", 
      getresources().getdrawable(r.drawable.ic_tab_unselected_starred)) 
    .setcontent(intent)); 
 } 
 
 /** 
  * sets the current tab based on the intent's request type 
  * 
  * @param intent intent that contains information about which tab should be selected 
  */ 
 private void setcurrenttab(intent intent) { 
  // dismiss menu provided by any children activities 
  activity activity = getlocalactivitymanager(). 
    getactivity(mtabhost.getcurrenttabtag()); 
  if (activity != null) { 
   activity.closeoptionsmenu(); 
  } 
 
  // tell the children activities that they should ignore any possible saved 
  // state and instead reload their state from the parent's intent 
  intent.putextra("", true); 
 
  // choose the tab based on the inbound intent 
  string componentname = intent.getcomponent().getclassname(); 
  if (getclass().getname().equals(componentname)) { 
   if (false) { 
    //in a call, show the dialer tab(which allows going back to the call) 
    mtabhost.setcurrenttab(tab_index_dialer); 
   } else if ((intent.getflags() & intent.flag_activity_launched_from_history) != 0) { 
    // launched from history (long-press home) --> nothing to change 
   } else if (true) { 
    // the dialer was explicitly requested 
    mtabhost.setcurrenttab(tab_index_dialer); 
   } 
  } 
 } 
} 

inner.java类:

package com.android.test; 
 
import android.app.tabactivity; 
import android.content.intent; 
import android.os.bundle; 
import android.view.window; 
import android.widget.tabhost; 
import android.widget.tabwidget; 
import android.widget.textview; 
 
public class inner extends tabactivity implements tabhost.ontabchangelistener { 
 private static final int tab_index_all = 0; 
 private static final int tab_index_missed = 1; 
 private static final int tab_index_outgoing = 2; 
 private static final int tab_index_received = 3; 
 
 private tabhost mtabhost; 
 private tabwidget mtabwidget; 
  
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  requestwindowfeature(window.feature_no_title); 
  setcontentview(r.layout.inner); 
 
  mtabhost = gettabhost(); 
  mtabhost.setontabchangedlistener(this); 
 
  setuptabs(); 
  mtabwidget = mtabhost.gettabwidget(); 
  mtabwidget.setstripenabled(false); 
 
  for (int i = 0; i < mtabwidget.getchildcount(); i++) { 
 
   textview tv = (textview) mtabwidget.getchildat(i).findviewbyid( 
     android.r.id.title); 
   tv.settextcolor(this.getresources().getcolorstatelist( 
     android.r.color.white)); 
    
   tv.setpadding(0, 0, 0,(int) tv.gettextsize()); 
   tv.settext("tab" + i); 
   mtabwidget.getchildat(i).getlayoutparams().height =(int ) (3* tv.gettextsize()); 
 
   mtabwidget.getchildat(i).setbackgroundresource(r.drawable.tab_bg); 
  } 
 } 
 
 public void ontabchanged(string tabid) { 
   
 } 
 
 private void setuptabs() { 
  mtabhost.addtab(mtabhost.newtabspec("all").setindicator( 
    getstring(r.string.inner)).setcontent( 
    new intent(this, other.class))); 
  mtabhost.addtab(mtabhost.newtabspec("missed").setindicator( 
    getstring(r.string.inner)).setcontent( 
    new intent(this, other.class))); 
  mtabhost.addtab(mtabhost.newtabspec("outgoing").setindicator( 
    getstring(r.string.inner)).setcontent( 
    new intent(this, other.class))); 
  mtabhost.addtab(mtabhost.newtabspec("received").setindicator( 
    getstring(r.string.inner)).setcontent( 
    new intent(this, other.class))); 
 
 } 
} 

效果图如下:

Android TabHost组件使用方法详解

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持。