歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中實現底部Tabhost【附源碼】

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

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

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