随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候想网易新闻要显示的内容太多,而且又想在主页面全部显示出来,所以有加了顶部导航栏,但是android这样的移动设备内存是受限的,那么多界面缓存到内存中,很容易导致内存溢出,这个是比较致命的,所以不得不考虑。虽然我在之前也做过网易的顶部导航栏但是哪种方式并不好,就像使用viewpager做一些复杂的界面由于图片占用内存过多,很容易导致内存溢出,学习了今天的内容大家做一下对比相信就有所体会。

先看一下今天要实现的效果:


至于顶部导航的具体要用到的图片和布局大家自己调整。

由于前面已经介绍了底部菜单栏了,所以一些重复性的代码就不贴上来了,最后我也会把下载地址贴上大家有兴趣自行下载。

首先看一些顶部导航栏的布局文件:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <includelayout="@layout/head"/>
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content">
  10. <RadioGroup
  11. android:id="@+id/add_tab_group"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:gravity="center"
  15. android:paddingTop="6dp"
  16. android:paddingBottom="6dp"
  17. android:background="@drawable/big_button_up"
  18. android:orientation="horizontal"
  19. >
  20. <RadioButton
  21. android:id="@+id/main_tab_addExam"
  22. style="@style/MMTabButton1"
  23. android:layout_weight="1.0"
  24. android:checked="true"
  25. android:text="添加考试"/>
  26. <RadioButton
  27. android:id="@+id/main_tab_myExam"
  28. style="@style/MMTabButton1"
  29. android:layout_weight="1.0"
  30. android:text="我的考试"/>
  31. <RadioButton
  32. android:id="@+id/main_tab_message"
  33. style="@style/MMTabButton1"
  34. android:layout_weight="1.0"
  35. android:text="我的通知"/>
  36. <RadioButton
  37. android:id="@+id/main_tab_testing"
  38. style="@style/MMTabButton1"
  39. android:layout_weight="1.0"
  40. android:text="测试"/>
  41. <RadioButton
  42. android:id="@+id/main_tab_settings"
  43. style="@style/MMTabButton1"
  44. android:layout_weight="1.0"
  45. android:text="设置"/>
  46. </RadioGroup>
  47. </LinearLayout>
  48. <LinearLayout
  49. android:id="@+id/container"
  50. android:layout_width="fill_parent"
  51. android:layout_height="fill_parent"
  52. android:layout_weight="1">
  53. </LinearLayout>
  54. </LinearLayout>

具体宽度样式大家可以自己调节,然后看一下核心类:

[html] view plain copy
  1. importandroid.app.ActivityGroup;
  2. importandroid.app.AlertDialog;
  3. importandroid.app.LocalActivityManager;
  4. importandroid.content.Context;
  5. importandroid.content.DialogInterface;
  6. importandroid.content.Intent;
  7. importandroid.os.Bundle;
  8. importandroid.view.KeyEvent;
  9. importandroid.view.View;
  10. importandroid.view.animation.Animation;
  11. importandroid.view.animation.AnimationUtils;
  12. importandroid.widget.Button;
  13. importandroid.widget.LinearLayout;
  14. importandroid.widget.RadioGroup;
  15. importandroid.widget.TextView;
  16. importandroid.widget.RadioGroup.OnCheckedChangeListener;
  17. publicclassAddExamActivityextendsActivityGroup{
  18. protectedButtonbtn_leftTop,btn_rightTop;
  19. protectedTextViewtv_head;
  20. privatestaticLocalActivityManagermanager;
  21. privateRadioGroupradioGroup;
  22. privatestaticLinearLayoutcontainer;
  23. publicstaticContextcontext;
  24. @Override
  25. protectedvoidonCreate(BundlesavedInstanceState){
  26. //TODOAuto-generatedmethodstub
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.addexam);
  29. context=this;
  30. initHead();
  31. manager=getLocalActivityManager();
  32. container=(LinearLayout)findViewById(R.id.container);
  33. radioGroup=(RadioGroup)this.findViewById(R.id.add_tab_group);
  34. container.removeAllViews();
  35. container.addView(manager.startActivity(
  36. "PAGE_0",
  37. newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  38. .getDecorView());
  39. radioGroup.setOnCheckedChangeListener(newOnCheckedChangeListener(){
  40. @Override
  41. publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
  42. //TODOAuto-generatedmethodstub
  43. switch(checkedId){
  44. caseR.id.main_tab_addExam://添加考试
  45. container.removeAllViews();
  46. container.addView(manager.startActivity(
  47. "PAGE_0",
  48. newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  49. .getDecorView());
  50. break;
  51. caseR.id.main_tab_myExam://我的考试
  52. container.removeAllViews();
  53. container.addView(manager.startActivity(
  54. "PAGE_1",
  55. newIntent(context,MyMessageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  56. .getDecorView());
  57. break;
  58. caseR.id.main_tab_message://我的通知
  59. container.removeAllViews();
  60. container.addView(manager.startActivity(
  61. "PAGE_2",
  62. newIntent(context,SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  63. .getDecorView());
  64. break;
  65. caseR.id.main_tab_testing://测试
  66. container.removeAllViews();
  67. container.addView(manager.startActivity(
  68. "PAGE_3",
  69. newIntent(context,TestingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  70. .getDecorView());
  71. break;
  72. caseR.id.main_tab_settings://设置
  73. container.removeAllViews();
  74. container.addView(manager.startActivity(
  75. "PAGE_4",
  76. newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  77. .getDecorView());
  78. break;
  79. default:
  80. //tabHost.setCurrentTabByTag("我的考试");
  81. break;
  82. }
  83. }
  84. });
  85. }
  86. publicstaticvoidchangeTo(){
  87. AnimationslideLeftIn=AnimationUtils.loadAnimation(context,R.anim.slide_bottom_in_no_alpha);
  88. container.removeAllViews();
  89. container.addView(manager.startActivity(
  90. "PAGE_4",
  91. newIntent(context,MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
  92. .getDecorView());
  93. container.startAnimation(slideLeftIn);
  94. }
  95. protectedvoidinitHead(){
  96. btn_leftTop=(Button)findViewById(R.id.btn_leftTop);
  97. btn_rightTop=(Button)findViewById(R.id.btn_rightTop);
  98. tv_head=(TextView)findViewById(R.id.tv_head);
  99. btn_leftTop.setVisibility(View.INVISIBLE);
  100. tv_head.setText("添加考试");
  101. }
  102. @Override
  103. publicbooleanonKeyDown(intkeyCode,KeyEventevent){
  104. //TODOAuto-generatedmethodstub
  105. if(keyCode==KeyEvent.KEYCODE_BACK){
  106. AlertDialog.Builderbuilder=newAlertDialog.Builder(getParent());
  107. builder.setMessage("你确定退出吗?")
  108. .setCancelable(false)
  109. .setPositiveButton("确定",
  110. newDialogInterface.OnClickListener(){
  111. publicvoidonClick(DialogInterfacedialog,
  112. intid){
  113. finish();
  114. System.exit(0);
  115. }
  116. })
  117. .setNegativeButton("返回",
  118. newDialogInterface.OnClickListener(){
  119. publicvoidonClick(DialogInterfacedialog,
  120. intid){
  121. dialog.cancel();
  122. }
  123. });
  124. AlertDialogalert=builder.create();
  125. alert.show();
  126. returntrue;
  127. }
  128. returnsuper.onKeyDown(keyCode,event);
  129. }
  130. }

这里继承了ActivityGroup,没有使用过的朋友从百度搜索下就明白了。

使用了LocalActivityManager启动子activity,这里Context和LinearLayout使用了static静态的,这是因为变态的需求使我不得不这样做,希望大家不要把这两个变量设置成static的,因为static的生命周期很长特别是Context不要设置成static,这样的话当前的activity很难被销毁的。其实使用tabhost完全可以实现,但是为什么没使用tabhost的我相信大家都明白,如果不考虑内存的话我也会使用,哈哈!


最后附上下载地址,有兴趣大家自己下载吧! 点击打开链接

更多相关文章

  1. Android(安卓)Developers:高效的加载大的位图
  2. 在android开发中应该如何管理内存或者是在开发过程中应该注意哪
  3. android LruCache的使用 (本地缓存+内存缓存)
  4. Android(安卓)图片内存缓存
  5. Android导航栏滑动折叠效果
  6. Android:Handler,内部类导致的可能内存泄露
  7. 《Pro Android(安卓)Graphics》读书笔记之第三节
  8. BottomNavigationView+ViewPager打造底部导航栏
  9. 内存优化二

随机推荐

  1. Android(安卓)UI学习系列
  2. android:name属性加不加“.”
  3. flutter包名,应用名称,图标,启动图片修改
  4. Android的版本的介绍
  5. 技巧: 如何安装apk文件在android仿真器中
  6. Android(安卓)Hello World
  7. Android(安卓)9.0 修改默认壁纸(主壁纸和w
  8. Android四种Activity的加载模式
  9. android移动数据上网的开关的实现
  10. android的BuildConfig学习