zoukankan      html  css  js  c++  java
  • Android底部菜单栏(用TabHost一次性加载耗内存)

    上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天把demo贴出来一是方便以后自己查询,二是希望同时也能帮到大家。

               底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏做。我这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了很多不必要的层,个人看起来还是不错的,所以贴出来方便以后使用)。

              先看一下做出来之后的效果:

     

           以后使用的时候就可以换成自己项目的图片和字体了,主框架不用变哈哈,

         首先是要布局layout下xml文件 main.xml:

    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. <LinearLayout
    7. android:layout_width="fill_parent"
    8. android:layout_height="fill_parent"
    9. android:background="@color/bg_gray"
    10. android:orientation="vertical" >
    11. <FrameLayout
    12. android:id="@android:id/tabcontent"
    13. android:layout_width="fill_parent"
    14. android:layout_height="0.0dip"
    15. android:layout_weight="1.0" />
    16. <TabWidget
    17. android:id="@android:id/tabs"
    18. android:layout_width="fill_parent"
    19. android:layout_height="wrap_content"
    20. android:layout_weight="0.0"
    21. android:visibility="gone" />
    22. <FrameLayout
    23. android:layout_width="fill_parent"
    24. android:layout_height="wrap_content" >
    25. <RadioGroup
    26. android:id="@+id/main_tab_group"
    27. android:layout_width="fill_parent"
    28. android:layout_height="wrap_content"
    29. android:layout_gravity="bottom"
    30. android:background="@drawable/bottom1"
    31. android:gravity="bottom"
    32. android:orientation="horizontal"
    33. >
    34. <RadioButton
    35. android:id="@+id/main_tab_addExam"
    36. style="@style/MMTabButton"
    37. android:layout_weight="1.0"
    38. android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
    39. android:text="添加考试" />
    40. <RadioButton
    41. android:id="@+id/main_tab_myExam"
    42. style="@style/MMTabButton"
    43. android:layout_weight="1.0"
    44. android:checked="true"
    45. android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
    46. android:text="我的考试" />
    47. <RadioButton
    48. android:id="@+id/main_tab_message"
    49. style="@style/MMTabButton"
    50. android:layout_weight="1.0"
    51. android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
    52. android:text="我的通知" />
    53. <RadioButton
    54. android:id="@+id/main_tab_settings"
    55. style="@style/MMTabButton"
    56. android:layout_weight="1.0"
    57. android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
    58. android:text="设置" />
    59. </RadioGroup>
    60. <TextView
    61. android:id="@+id/main_tab_new_message"
    62. android:layout_width="wrap_content"
    63. android:layout_height="wrap_content"
    64. android:layout_gravity="center_horizontal|top"
    65. android:layout_marginLeft="60dip"
    66. android:layout_marginTop="1dip"
    67. android:background="@drawable/tips"
    68. android:gravity="center"
    69. android:text="1"
    70. android:textColor="#ffffff"
    71. android:textSize="10sp"
    72. android:visibility="gone" />
    73. </FrameLayout>
    74. </LinearLayout>
    75. </TabHost>
     在RadioGroup的外面加了一个FrameLayout,主要是为了使用TextView显示消息数量,这里是居中靠左60dip,可能你会问直接写死能支持多分辨率吗,这个我在320*480的手机上试过没问题的,因为dip是与设备无关的支持多分辨率,至于1280*800平板电脑这样的分辨率我就不能保证了,哈哈!

          接下来是样式布局:

    1. <style name="MMTabButton">
    2. <item name="android:textSize">12.0dip</item>
    3. <item name="android:gravity">center_horizontal</item>
    4. <item name="android:background">@drawable/bg_checkbox_menus</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:textColor">@color/white</item>
    9. <item name="android:layout_weight">1.0</item>
    10. <item name="android:paddingBottom">2.0dip</item>
    11. <item name="android:paddingTop">2.0dip</item>
    12. </style>
    在drawable下bg_checkbox_menus.xml
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item
    4. android:state_checked="false"
    5. android:drawable="@drawable/mm_trans" />
    6. <item
    7. android:state_checked="true"
    8. android:drawable="@drawable/home_btn_bg" />
    9. </selector>
    其他的那四个都合这个一样点击后图片换成亮色的,所以就不一一贴出来了。

         最后看MainActivity这个类:

    1. package cn.com.karl.test;
    2. import android.app.TabActivity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.Window;
    7. import android.widget.RadioGroup;
    8. import android.widget.RadioGroup.OnCheckedChangeListener;
    9. import android.widget.TabHost;
    10. import android.widget.TextView;
    11. public class MainActivity extends TabActivity {
    12. /** Called when the activity is first created. */
    13. private TabHost tabHost;
    14. private TextView main_tab_new_message;
    15. @Override
    16. public void onCreate(Bundle savedInstanceState) {
    17. super.onCreate(savedInstanceState);
    18. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    19. setContentView(R.layout.main);
    20. main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
    21. main_tab_new_message.setVisibility(View.VISIBLE);
    22. main_tab_new_message.setText("10");
    23. tabHost=this.getTabHost();
    24. TabHost.TabSpec spec;
    25. Intent intent;
    26. intent=new Intent().setClass(this, AddExamActivity.class);
    27. spec=tabHost.newTabSpec("添加考试").setIndicator("添加考试").setContent(intent);
    28. tabHost.addTab(spec);
    29. intent=new Intent().setClass(this,MyExamActivity.class);
    30. spec=tabHost.newTabSpec("我的考试").setIndicator("我的考试").setContent(intent);
    31. tabHost.addTab(spec);
    32. intent=new Intent().setClass(this, MyMessageActivity.class);
    33. spec=tabHost.newTabSpec("我的通知").setIndicator("我的通知").setContent(intent);
    34. tabHost.addTab(spec);
    35. intent=new Intent().setClass(this, SettingActivity.class);
    36. spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);
    37. tabHost.addTab(spec);
    38. tabHost.setCurrentTab(1);
    39. RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
    40. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    41. @Override
    42. public void onCheckedChanged(RadioGroup group, int checkedId) {
    43. // TODO Auto-generated method stub
    44. switch (checkedId) {
    45. case R.id.main_tab_addExam://添加考试
    46. tabHost.setCurrentTabByTag("添加考试");
    47. break;
    48. case R.id.main_tab_myExam://我的考试
    49. tabHost.setCurrentTabByTag("我的考试");
    50. break;
    51. case R.id.main_tab_message://我的通知
    52. tabHost.setCurrentTabByTag("我的通知");
    53. break;
    54. case R.id.main_tab_settings://设置
    55. tabHost.setCurrentTabByTag("设置");
    56. break;
    57. default:
    58. //tabHost.setCurrentTabByTag("我的考试");
    59. break;
    60. }
    61. }
    62. });
    63. }
    64. }
      这样就完成了,主要还是使用了tabhost完成,tabhost有缓存机制这四个界面都会缓存到内存中,这样即有利也有弊,有利是因为切换的时候不用在重新加载了,有弊是因为缓存四个界面会耗费内存较多一些。如果只想缓存一个界面以后下一篇我会使用ActivityGroup实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。

            最后奉上下载地址,如果有需要的希望大家自己去下载吧,有些代码存在本地时间长了我也会弄丢。[rar文件] android底部菜单栏demo


     
     



    附件列表

    • 相关阅读:
      索引
      varnish它是一款高性能的http缓存服务器
      前端性能优化浅谈
      锁机制-SQL Server 数据库
      NET Core中NuGet包
      “干掉” if...else
      混搭.NET技术
      MONO x64 amd_x64
      跨平台移动开发UI语言 -XAML
      使用LinqToExcel读取Excel
    • 原文地址:https://www.cnblogs.com/superit/p/4118133.html
    Copyright © 2011-2022 走看看