歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TAb分頁菜單實現總結

Android TAb分頁菜單實現總結

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

首先說明的是,我們做APP開發,Tab分頁不管是頂部還是底部,都是必不可少的,網上也有太多太多的實現方式了,我在這裡總結一下:

第一種方式: TabHost原始方式:(鏈接另一篇文章 http://www.linuxidc.com/Linux/2012-08/69303.htm )

這裡實現的是底部菜單:

布局文件:(我們通過RelativeLayout 可以把TabWidget定位在底部)

  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="fill_parent"
  5. android:layout_height="fill_parent" >
  6. <RelativeLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:orientation="vertical"
  10. android:padding="3dp" >
  11. <FrameLayout
  12. android:id="@android:id/tabcontent"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:layout_weight="1" >
  16. </FrameLayout>
  17. <TabWidget
  18. android:id="@android:id/tabs"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_alignBottom="@android:id/tabcontent"
  22. android:background="@drawable/tabbar_bg" />
  23. </RelativeLayout>
  24. </TabHost>

在這裡我們將說明一下:之前我是獲取到TabWidget的view試圖及內部icon和title,然後控制實現其效果,但是我們也可以用另外一種方式,也就是我們調用TabHost.TabSpec 的setIndicator(View view);這個方法,我們可以定制顯示的view,

代碼片段:

  1. /***
  2. * 創建footerview
  3. */
  4. public void createFooterView() {
  5. tabHost = getTabHost(); // The activity TabHost
  6. view = new TabView(this, R.drawable.tabbar_icon_home,
  7. R.drawable.tabbar_icon_home_selecotr);
  8. view.setBackgroundDrawable(this.getResources().getDrawable(
  9. R.drawable.footer_view_selector));
  10. intent = new Intent(MainActivity.this, HomeActivity.class);
  11. spec = tabHost.newTabSpec("num1").setIndicator(view).setContent(intent);
  12. tabHost.addTab(spec);
  13. view = new TabView(this, R.drawable.tabbar_icon_search,
  14. R.drawable.tabbar_icon_search_selecotr);
  15. view.setBackgroundDrawable(this.getResources().getDrawable(
  16. R.drawable.footer_view_selector));
  17. intent = new Intent(MainActivity.this, HomeActivity.class);
  18. spec = tabHost.newTabSpec("num2").setIndicator(view).setContent(intent);
  19. tabHost.addTab(spec);
  20. view = new TabView(this, R.drawable.tabbar_icon_cart,
  21. R.drawable.tabbar_icon_cart_selector);
  22. view.setBackgroundDrawable(this.getResources().getDrawable(
  23. R.drawable.footer_view_selector));
  24. intent = new Intent(MainActivity.this, HomeActivity.class);
  25. spec = tabHost.newTabSpec("num3").setIndicator(view).setContent(intent);
  26. tabHost.addTab(spec);
  27. view = new TabView(this, R.drawable.tabbar_icon_more,
  28. R.drawable.tabbar_icon_more_selecotr);
  29. view.setBackgroundDrawable(this.getResources().getDrawable(
  30. R.drawable.footer_view_selector));
  31. intent = new Intent(MainActivity.this, HomeActivity.class);
  32. spec = tabHost.newTabSpec("num4").setIndicator(view).setContent(intent);
  33. tabHost.addTab(spec);
  34. }
  1. /***
  2. * 自定義view
  3. *
  4. */
  5. class TabView extends LinearLayout {
  6. ImageView imageView;
  7. public TabView(Context c, int drawable, int drawableselec) {
  8. super(c);
  9. imageView = new ImageView(c);
  10. // 可以定制點擊後狀態
  11. StateListDrawable listDrawable = new StateListDrawable();
  12. // 未選
  13. listDrawable.addState(SELECTED_STATE_SET, this.getResources()
  14. .getDrawable(drawableselec));
  15. // 選擇
  16. listDrawable.addState(ENABLED_STATE_SET, this.getResources()
  17. .getDrawable(drawable));
  18. imageView.setImageDrawable(listDrawable);// 引用 StateListDrawable
  19. setGravity(Gravity.CENTER);
  20. addView(imageView);
  21. }
  22. }

這樣我們就實現想要的效果了.(建議使用這種方法,我的項目就是用的這個實現的.)
如果我是圖標和文字分開的,我們也可以用(RadioButton代替,也許大家都不陌生,一會我簡單介紹下)


這個源碼是因為項目裡面用的。

Copyright © Linux教程網 All Rights Reserved