Android项目实战之仿网易顶部导航栏效果

所属分类: 软件编程 / Android 阅读数: 908
收藏 0 赞 0 分享

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 <include layout="@layout/head" /> 
 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <RadioGroup 
 android:id="@+id/add_tab_group" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center" 
 android:paddingTop="6dp" 
 android:paddingBottom="6dp" 
 android:background="@drawable/big_button_up" 
 android:orientation="horizontal" 
  > 
 
 <RadioButton 
  android:id="@+id/main_tab_addExam" 
  style="@style/MMTabButton1" 
  android:layout_weight="1.0" 
  android:checked="true" 
  android:text="添加考试" /> 
 
 <RadioButton 
  android:id="@+id/main_tab_myExam" 
  style="@style/MMTabButton1" 
  android:layout_weight="1.0" 
  
  android:text="我的考试" /> 
 
 <RadioButton 
  android:id="@+id/main_tab_message" 
  style="@style/MMTabButton1" 
  android:layout_weight="1.0" 
  android:text="我的通知" /> 
 
 <RadioButton 
  android:id="@+id/main_tab_testing" 
  style="@style/MMTabButton1" 
  android:layout_weight="1.0" 
  android:text="测试" /> 
 <RadioButton 
  android:id="@+id/main_tab_settings" 
  style="@style/MMTabButton1" 
  android:layout_weight="1.0" 
  android:text="设置" /> 
 </RadioGroup> 
 
 </LinearLayout> 
 
 <LinearLayout 
 android:id="@+id/container" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_weight="1" > 
 </LinearLayout> 
</LinearLayout> 

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

import android.app.ActivityGroup; 
import android.app.AlertDialog; 
import android.app.LocalActivityManager; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
 
public class AddExamActivity extends ActivityGroup { 
 
 protected Button btn_leftTop, btn_rightTop; 
 protected TextView tv_head; 
 
 private static LocalActivityManager manager; 
 private RadioGroup radioGroup; 
 private static LinearLayout container; 
 public static Context context; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 // TODO Auto-generated method stub 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.addexam); 
 context=this; 
 initHead(); 
 
 manager=getLocalActivityManager(); 
 container= (LinearLayout)findViewById(R.id.container); 
 radioGroup=(RadioGroup) this.findViewById(R.id.add_tab_group); 
 
 container.removeAllViews(); 
 container.addView(manager.startActivity( 
 "PAGE_0", 
 new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
 .getDecorView()); 
 
 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
 
 @Override 
 public void onCheckedChanged(RadioGroup group, int checkedId) { 
 // TODO Auto-generated method stub 
 switch (checkedId) { 
 case R.id.main_tab_addExam://添加考试 
  container.removeAllViews(); 
  container.addView(manager.startActivity( 
  "PAGE_0", 
  new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
  .getDecorView()); 
  break; 
 case R.id.main_tab_myExam://我的考试 
  container.removeAllViews(); 
  container.addView(manager.startActivity( 
  "PAGE_1", 
  new Intent(context, MyMessageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
  .getDecorView()); 
  break; 
 case R.id.main_tab_message://我的通知 
  container.removeAllViews(); 
  container.addView(manager.startActivity( 
  "PAGE_2", 
  new Intent(context, SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
  .getDecorView()); 
  break; 
 case R.id.main_tab_testing://测试 
  container.removeAllViews(); 
  container.addView(manager.startActivity( 
  "PAGE_3", 
  new Intent(context, TestingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
  .getDecorView()); 
  break; 
 case R.id.main_tab_settings://设置 
  container.removeAllViews(); 
  container.addView(manager.startActivity( 
  "PAGE_4", 
  new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
  .getDecorView()); 
  break; 
 default: 
  //tabHost.setCurrentTabByTag("我的考试"); 
  break; 
 } 
 } 
 }); 
 } 
 public static void changeTo(){ 
 Animation slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_in_no_alpha); 
 
 container.removeAllViews(); 
 container.addView(manager.startActivity( 
 "PAGE_4", 
 new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
 .getDecorView()); 
 container.startAnimation(slideLeftIn); 
 } 
 protected void initHead() { 
 btn_leftTop = (Button) findViewById(R.id.btn_leftTop); 
 btn_rightTop = (Button) findViewById(R.id.btn_rightTop); 
 tv_head = (TextView) findViewById(R.id.tv_head); 
 
 btn_leftTop.setVisibility(View.INVISIBLE); 
 tv_head.setText("添加考试"); 
 } 
 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) { 
 // TODO Auto-generated method stub 
 
 if (keyCode == KeyEvent.KEYCODE_BACK) { 
 
 AlertDialog.Builder builder = new AlertDialog.Builder(getParent()); 
 builder.setMessage("你确定退出吗?") 
  .setCancelable(false) 
  .setPositiveButton("确定", 
  new DialogInterface.OnClickListener() { 
  public void onClick(DialogInterface dialog, 
   int id) { 
   finish(); 
   System.exit(0); 
  } 
  }) 
  .setNegativeButton("返回", 
  new DialogInterface.OnClickListener() { 
  public void onClick(DialogInterface dialog, 
   int id) { 
   dialog.cancel(); 
  } 
  }); 
 AlertDialog alert = builder.create(); 
 alert.show(); 
 return true; 
 } 
 
 return super.onKeyDown(keyCode, event); 
 } 
} 

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

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

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多