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

来源: <http://blog.csdn.net/yuzhiboyi/article/details/7782276#comments>

来自为知笔记(Wiz)

附件列表

时间: 2024-08-28 21:04:43

Android底部菜单栏(用TabHost一次性加载耗内存)的相关文章

Android 下拉刷新上拉加载 多种应用场景 超级大放送(上)

转载请标明原文地址:http://blog.csdn.net/yalinfendou/article/details/47707017 关于Android下拉刷新上拉加载,网上的Demo太多太多了,这里不是介绍怎么去实现下拉刷新上拉加载,而是针对下拉刷新上拉加载常用的一些应用场景就行了一些总结,包含了下拉刷新上拉加载过程中遇到的一些手势冲突问题的解决方法(只能算是抛砖引玉). 去年9月的时候,那时自己正在独立做Android项目.记得刚刚写完那个ListView列表页面(木有下拉刷新,上拉加载)

Android 下拉刷新上拉加载效果功能,使用开源项目android-pulltorefresh实现

应用场景: 在App开发中,对于信息的获取与演示,不可能全部将其获取与演示,为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态加载数据的要求就会出现.为此,该效果功能就需要应用到所需要的展示页面中. 知识点介绍: 本文主要根据开源项目android-pulltorefresh展开介绍. android-pulltorefresh [一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.ViewPager.WevView.ExpandableListView.G

Android 下拉刷新上拉加载效果功能

应用场景: 在App开发中,对于信息的获取与演示,不可能全部将其获取与演示,为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态加载数据的要求就会出现.为此,该效果功能就需要应用到所需要的展示页面中. 知识点介绍: 本文主要根据开源项目android-pulltorefresh展开介绍. android-pulltorefresh [一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.ViewPager.WevView.ExpandableListView.G

Android插件化(三)加载插件apk中的Resource资源

Android加载插件apk中的Resource资源 简介 如何加载未安装apk中的资源文件呢?我们从android.content.res.AssetManager.java的源码中发现,它有一个私有方法addAssetPath,只需要将apk的路径作为参数传入,我们就可以获得对应的AssetsManager对象,然后我们就可以使用AssetsManager对象,创建一个Resources对象,然后就可以从Resource对象中访问apk中的资源了.总结如下: 1.新建一个AssetManag

[Android学习系列16]Android把php输出的json加载到listview

首先写个php脚本输出json,注意,还要输出回车,方便android的bufferreader读取一行 <?php class Book { public $bookid; public $bookname; public $bookinfo; function __construct($id,$name,$info ){ $this->bookid = $id; $this->bookname = $name; $this->bookinfo = $info; } } $boo

Android App 启动时显示正在加载图片(源码)

微信.QQ.天天动听等程序,在打开时显示了一张图片,然后跳转到相关界面.本文实现这个功能,其实很简单.... 新建两个Activity,LoadingActivity,MainActivity,将LoadingActivity设置为android.intent.action.MAIN.使用TimerTesk,或者Thread将LoadingActivity显示几秒后跳转到MainActivity界面. LoadingActivity: new Timer().schedule(new Timer

android 下拉刷新上拉加载更多,高仿ios左滑动删除item,解决了众多手势问题

一.前言 老规矩,别的不说,这demo是找了很相关知识集合而成的,可以说对我这种小白来说是绞尽脑汁!程序员讲的是无图无真相!现在大家一睹为快! 二.比较关键的还是scroller这个类的 package com.icq.slideview.view; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; i

Android下将图片加载到内存中

Android的系统的标准默认每个应用程序分配的内存是16M.所以来说是非常宝贵的,在创建应用的时候要尽可能的去节省内存,但是在加载一些大的文件的时候,比如图片是相当耗内存的,一个1.3M的图片,分辨率是2560X1920(宽X高)图片当加载到手机内存的时候就会请求19M的一块内存,这是远远超出了系统自带的内存空间,这时候应用程序就会挂掉,所以我们要进行图片的缩放处理,下面我就来带大家创建一个用来图片缩放的应用: 应用效果图如下: 核心代码的实现: package com.examp.loadp

★android开发--ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)

例子中用于解析Json的Gson请自己Google下载 主Activity: package COM.Example.Main; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import COM.Example.Main.R; import COM.Example.Main.stringG