歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android中實現底部Tabhost【附源碼】

這個主要是實現底部的tabhost方式,tabhost就是有幾個標簽滑動的一個控件。activity繼承TabActivity
其他不多說了,直接上代碼

  1. public class main extends TabActivity {   
  2.     private TabHost tabHost;   
  3.     private TabWidget tabWidget;   
  4.     Field mBottomLeftStrip;   
  5.     Field mBottomRightStrip;   
  6.   
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {   
  9.         super.onCreate(savedInstanceState);   
  10.         setContentView(R.layout.main);   
  11.   
  12.         makeTab();   
  13.     }   
  14.   
  15.     public void makeTab() {   
  16.         if (this.tabHost == null) {   
  17.             tabHost = getTabHost();   
  18.             tabWidget = getTabWidget();   
  19.             tabHost.setup();   
  20.             tabHost.bringToFront();   
  21.   
  22.             TabSpec firsttab = tabHost.newTabSpec("firsttab");   
  23.             TabSpec sencondtab = tabHost.newTabSpec("sencondtab");   
  24.             TabSpec thirdtab = tabHost.newTabSpec("thirdtab");   
  25.   
  26.             firsttab.setIndicator("first",   
  27.                     getResources().getDrawable(R.drawable.first)).setContent(   
  28.                     new Intent(this, first.class));   
  29.             sencondtab.setIndicator("second",   
  30.                     getResources().getDrawable(R.drawable.second)).setContent(   
  31.                     new Intent(this, second.class));   
  32.             thirdtab.setIndicator("third",   
  33.                     getResources().getDrawable(R.drawable.third)).setContent(   
  34.                     new Intent(this, third.class));   
  35.   
  36.             tabHost.addTab(firsttab);   
  37.             tabHost.addTab(sencondtab);   
  38.             tabHost.addTab(thirdtab);   
  39.   
  40.             if (Integer.valueOf(Build.VERSION.SDK) <= 7) {   
  41.                 try {   
  42.                     mBottomLeftStrip = tabWidget.getClass().getDeclaredField(   
  43.                             "mBottomLeftStrip");   
  44.                     mBottomRightStrip = tabWidget.getClass().getDeclaredField(   
  45.                             "mBottomRightStrip");   
  46.                     if (!mBottomLeftStrip.isAccessible()) {   
  47.                         mBottomLeftStrip.setAccessible(true);   
  48.                     }   
  49.                     if (!mBottomRightStrip.isAccessible()) {   
  50.                         mBottomRightStrip.setAccessible(true);   
  51.                     }   
  52.                     mBottomLeftStrip.set(tabWidget,   
  53.                             getResources().getDrawable(R.drawable.linee));   
  54.                     mBottomRightStrip.set(tabWidget, getResources()   
  55.                             .getDrawable(R.drawable.linee));   
  56.   
  57.                 } catch (Exception e) {   
  58.                     e.printStackTrace();   
  59.                 }   
  60.             } else {   
  61.                 try {   
  62.                     mBottomLeftStrip = tabWidget.getClass().getDeclaredField(   
  63.                             "mLeftStrip");   
  64.                     mBottomRightStrip = tabWidget.getClass().getDeclaredField(   
  65.                             "mRightStrip");   
  66.                     if (!mBottomLeftStrip.isAccessible()) {   
  67.                         mBottomLeftStrip.setAccessible(true);   
  68.                     }   
  69.                     if (!mBottomRightStrip.isAccessible()) {   
  70.                         mBottomRightStrip.setAccessible(true);   
  71.                     }   
  72.                     mBottomLeftStrip.set(tabWidget,   
  73.                             getResources().getDrawable(R.drawable.linee));   
  74.                     mBottomRightStrip.set(tabWidget, getResources()   
  75.                             .getDrawable(R.drawable.linee));   
  76.                 } catch (Exception e) {   
  77.                     e.printStackTrace();   
  78.                 }   
  79.             }   
  80.   
  81.             for (int i = 0; i < tabWidget.getChildCount(); i++) {   
  82.   
  83.                 View view = tabWidget.getChildAt(i);   
  84.                 if (tabHost.getCurrentTab() == i) {   
  85.                     view.setBackgroundDrawable(getResources().getDrawable(   
  86.                             R.drawable.focus));   
  87.                 } else {   
  88.                     view.setBackgroundDrawable(getResources().getDrawable(   
  89.                             R.drawable.unfocus));   
  90.                 }   
  91.             }   
  92.   
  93.             tabHost.setOnTabChangedListener(new OnTabChangeListener() {   
  94.   
  95.                 @Override  
  96.                 public void onTabChanged(String tabId) {   
  97.                     for (int i = 0; i < tabWidget.getChildCount(); i++) {   
  98.                         View view = tabWidget.getChildAt(i);   
  99.                         Toast.makeText(main.this, tabId, Toast.LENGTH_SHORT).show();   
  100.                         if (tabHost.getCurrentTab() == i) {   
  101.                             view.setBackgroundDrawable(getResources()   
  102.                                     .getDrawable(R.drawable.focus));   
  103.                         } else {   
  104.                             view.setBackgroundDrawable(getResources()   
  105.                                     .getDrawable(R.drawable.unfocus));   
  106.                         }   
  107.                     }   
  108.                 }   
  109.             });   
  110.         }   
  111.     }   
  112. }  
Copyright © Linux教程網 All Rights Reserved