利用TabWidget实现底部菜单

迷南。 2022-07-28 04:05 254阅读 0赞

TabWidget类似于通话记录的界面,通过切换多个标签从而显示出多个不同内容,能够展示内容丰富的页面信息,而且彼此之间不会干扰,有利于展示。下面,通过一个例子来学习用法

首先用一个类来继承TabActivity

在开发之前,我们要首先了解,TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。接着我们开始初始化main.xml。
首先声明TabHost,包含TabWidget,FrameLayout元素。

  1. <TabHost
  2. android:id="@android:id/tabhost" //声明控件ID
  3. android:layout_width="fill_parent" //控件宽度与父控件一致
  4. android:layout_height="fill_parent"> //控件高度与父控件一致
  5. 声明TabWidget,tab标签页
  6. <TabWidget
  7. android:layout_width="fill_parent" //控件宽度与父控件一致
  8. android:layout_height="wrap_content" //控件高度与自身适应
  9. android:id="@android:id/tabs"> //声明控件ID
  10. 声明FrameLayout,tab页里的内容信息
  11. <FrameLayout
  12. android:layout_width="fill_parent" //控件宽度与父控件一致
  13. android:layout_height="wrap_content" //控件高度与自身适应
  14. android:id="@android:id/tabcontent"> //声明控件ID

注意下:
如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
TabWidget必须设置android:id为@android:id/tabs
FrameLayout需要设置android:id为@android:id/tabcontent

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@android:id/tabhost" >
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:orientation="vertical" >
  10. <FrameLayout android:id="@android:id/tabcontent"
  11. android:layout_width="fill_parent"
  12. android:layout_height="0.0dip"
  13. android:layout_weight="1.0"/>
  14. <TabWidget android:id="@android:id/tabs"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:visibility="gone"
  18. />
  19. <RadioGroup
  20. android:id="@+id/tab_items"
  21. android:gravity="center_vertical"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="horizontal"
  25. android:layout_gravity="bottom"
  26. android:background="@drawable/tab_bg"
  27. >
  28. <RadioButton
  29. android:id="@+id/tab_item_home"
  30. android:checked="true"
  31. style="@style/main_tab_bottom"
  32. android:background="@drawable/item_home_bg" />
  33. <RadioButton
  34. android:id="@+id/tab_item_nearby"
  35. style="@style/main_tab_bottom"
  36. android:background="@drawable/item_near_bg"
  37. />
  38. <RadioButton
  39. android:id="@+id/tab_item_sort"
  40. style="@style/main_tab_bottom"
  41. android:background="@drawable/item_sort_bg" />
  42. <RadioButton
  43. android:id="@+id/tab_item_mine"
  44. style="@style/main_tab_bottom"
  45. android:background="@drawable/item_mine_bg"/>
  46. <RadioButton
  47. android:id="@+id/tab_item_more"
  48. style="@style/main_tab_bottom"
  49. android:background="@drawable/item_more_bg" />
  50. </RadioGroup>
  51. </LinearLayout>
  52. </TabHost>

其中有些控件的图片点击与正常情况下是不同的,如item_home_bg.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_checked="true" android:drawable="@drawable/but_index_r_v2" />
  4. <item android:drawable="@drawable/but_index_v2"/>
  5. </selector>

style文件在values文件夹下的styles.xml文件中定义

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="main_tab_bottom">
  4. <item name="android:gravity">center_horizontal</item>
  5. <item name="android:layout_width">fill_parent</item>
  6. <item name="android:layout_height">wrap_content</item>
  7. <item name="android:button">@null</item>
  8. <item name="android:layout_weight">1.0</item>
  9. </style>
  10. </resources>

函数实现

  1. public class MyTab extends TabActivity{
  2. private final static String TAG = "TabShow";
  3. private TabHost mHost;
  4. private RadioGroup tabItems;
  5. private RadioButton mineBut;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. // TODO Auto-generated method stub
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.tablay);
  11. initResourceRefs();
  12. initSettings();
  13. }
  14. private void initSettings() {
  15. // TODO Auto-generated method stub
  16. tabItems.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  17. @Override
  18. public void onCheckedChanged(RadioGroup group, int checkedId) {
  19. // TODO Auto-generated method stub
  20. switch(checkedId){
  21. case R.id.tab_item_home :
  22. mHost.setCurrentTabByTag("HOME");
  23. break;
  24. case R.id.tab_item_nearby :
  25. mHost.setCurrentTabByTag("NEAR");
  26. break;
  27. case R.id.tab_item_sort :
  28. mHost.setCurrentTabByTag("SORT");
  29. break;
  30. case R.id.tab_item_more :
  31. mHost.setCurrentTabByTag("MORE");
  32. break;
  33. }
  34. }
  35. });
  36. }
  37. private void initResourceRefs() {
  38. // TODO Auto-generated method stub
  39. mHost = getTabHost();
  40. mHost.addTab(mHost.newTabSpec("HOME").setIndicator("HOME")
  41. .setContent(new Intent(this , HomeActivity.class)));
  42. mHost.addTab(mHost.newTabSpec("NEAR").setIndicator("NEAR")
  43. .setContent(new Intent(this , NearByActivity.class)));
  44. mHost.addTab(mHost.newTabSpec("SORT").setIndicator("SORT")
  45. .setContent(new Intent(this , SortActivity.class)));
  46. mHost.addTab(mHost.newTabSpec("My").setIndicator("My")
  47. .setContent(new Intent(this , MyActivity.class)));
  48. mHost.addTab(mHost.newTabSpec("MORE").setIndicator("MORE")
  49. .setContent(new Intent(this , MoreActivity.class)));
  50. tabItems = (RadioGroup)findViewById(R.id.tab_items);
  51. mineBut = (RadioButton)findViewById(R.id.tab_item_mine);
  52. }
  53. }

效果如下

如下

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MUI底部二级菜单

    引言 底部二级菜单,笔者在原生的APP很少看到该功能,而在微信公众号倒是挺常见的,不过既然在MUI中看到此技巧,那笔者就在此记录一下方便自己日后查阅,同时也希望可以帮...

    相关 TabWidget

    TabWidget通过多个标签切换显示不同的内容。要实现这一效果,需要先了解TabHost,它是一个存放多个Tab标签的容器。每个Tab都可以对应自己的布局。 1 在一个容

    相关 利用TabWidget实现底部菜单

    TabWidget类似于通话记录的界面,通过切换多个标签从而显示出多个不同内容,能够展示内容丰富的页面信息,而且彼此之间不会干扰,有利于展示。下面,通过一个例子来学习用法