android网易顶部导航栏demo .

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

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

     

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

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

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.     <include layout="@layout/head" />  
  7.      
  8.   <LinearLayout   
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content">  
  11.             <RadioGroup  
  12.                 android:id="@+id/add_tab_group"  
  13.                 android:layout_width="fill_parent"  
  14.                 android:layout_height="wrap_content"  
  15.                 android:gravity="center"  
  16.                 android:paddingTop="6dp"  
  17.                 android:paddingBottom="6dp"  
  18.                 android:background="@drawable/big_button_up"  
  19.                 android:orientation="horizontal"  
  20.                  >  
  21.   
  22.                 <RadioButton  
  23.                     android:id="@+id/main_tab_addExam"  
  24.                     style="@style/MMTabButton1"  
  25.                     android:layout_weight="1.0"  
  26.                     android:checked="true"  
  27.                     android:text="添加考试" />  
  28.   
  29.                 <RadioButton  
  30.                     android:id="@+id/main_tab_myExam"  
  31.                     style="@style/MMTabButton1"  
  32.                     android:layout_weight="1.0"  
  33.                      
  34.                     android:text="我的考试" />  
  35.   
  36.                 <RadioButton  
  37.                     android:id="@+id/main_tab_message"  
  38.                     style="@style/MMTabButton1"  
  39.                     android:layout_weight="1.0"  
  40.                     android:text="我的通知" />  
  41.   
  42.                 <RadioButton  
  43.                     android:id="@+id/main_tab_testing"  
  44.                     style="@style/MMTabButton1"  
  45.                     android:layout_weight="1.0"  
  46.                     android:text="测试" />  
  47.                 <RadioButton  
  48.                     android:id="@+id/main_tab_settings"  
  49.                     style="@style/MMTabButton1"  
  50.                     android:layout_weight="1.0"  
  51.                     android:text="设置" />  
  52.             </RadioGroup>  
  53.               
  54.        </LinearLayout>  
  55.          
  56.     <LinearLayout  
  57.         android:id="@+id/container"  
  58.         android:layout_width="fill_parent"  
  59.         android:layout_height="fill_parent"  
  60.         android:layout_weight="1" >  
  61.     </LinearLayout>  
  62. </LinearLayout>  
      具体宽度样式大家可以自己调节,然后看一下核心类:

  

  1. import android.app.ActivityGroup;  
  2. import android.app.AlertDialog;  
  3. import android.app.LocalActivityManager;  
  4. import android.content.Context;  
  5. import android.content.DialogInterface;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.KeyEvent;  
  9. import android.view.View;  
  10. import android.view.animation.Animation;  
  11. import android.view.animation.AnimationUtils;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.RadioGroup;  
  15. import android.widget.TextView;  
  16. import android.widget.RadioGroup.OnCheckedChangeListener;  
  17.   
  18. public class AddExamActivity extends ActivityGroup {  
  19.   
  20.     protected Button btn_leftTop, btn_rightTop;  
  21.     protected TextView tv_head;  
  22.       
  23.     private  static LocalActivityManager manager;  
  24.     private  RadioGroup radioGroup;  
  25.     private  static LinearLayout container;  
  26.     public  static Context context;  
  27.       
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         // TODO Auto-generated method stub  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.addexam);  
  33.         context=this;  
  34.         initHead();  
  35.           
  36.         manager=getLocalActivityManager();  
  37.         container= (LinearLayout)findViewById(R.id.container);  
  38.         radioGroup=(RadioGroup) this.findViewById(R.id.add_tab_group);  
  39.           
  40.         container.removeAllViews();  
  41.         container.addView(manager.startActivity(  
  42.                 "PAGE_0",  
  43.                 new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  44.                 .getDecorView());  
  45.           
  46.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  47.               
  48.             @Override  
  49.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  50.                 // TODO Auto-generated method stub  
  51.                 switch (checkedId) {  
  52.                 case R.id.main_tab_addExam://添加考试  
  53.                     container.removeAllViews();  
  54.                     container.addView(manager.startActivity(  
  55.                             "PAGE_0",  
  56.                             new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  57.                             .getDecorView());  
  58.                     break;  
  59.                 case R.id.main_tab_myExam://我的考试  
  60.                     container.removeAllViews();  
  61.                     container.addView(manager.startActivity(  
  62.                             "PAGE_1",  
  63.                             new Intent(context, MyMessageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  64.                             .getDecorView());  
  65.                     break;  
  66.                 case R.id.main_tab_message://我的通知  
  67.                     container.removeAllViews();  
  68.                     container.addView(manager.startActivity(  
  69.                             "PAGE_2",  
  70.                             new Intent(context, SettingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  71.                             .getDecorView());  
  72.                     break;  
  73.                 case R.id.main_tab_testing://测试  
  74.                     container.removeAllViews();  
  75.                     container.addView(manager.startActivity(  
  76.                             "PAGE_3",  
  77.                             new Intent(context, TestingActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  78.                             .getDecorView());  
  79.                     break;  
  80.                 case R.id.main_tab_settings://设置  
  81.                     container.removeAllViews();  
  82.                     container.addView(manager.startActivity(  
  83.                             "PAGE_4",  
  84.                             new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  85.                             .getDecorView());  
  86.                     break;  
  87.                 default:  
  88.                     //tabHost.setCurrentTabByTag("我的考试");  
  89.                     break;  
  90.                 }  
  91.             }  
  92.         });  
  93.     }  
  94.     public static void changeTo(){  
  95.         Animation slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_in_no_alpha);  
  96.           
  97.         container.removeAllViews();  
  98.         container.addView(manager.startActivity(  
  99.                 "PAGE_4",  
  100.                 new Intent(context, MyExamActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  101.                 .getDecorView());  
  102.         container.startAnimation(slideLeftIn);  
  103.     }  
  104.     protected void initHead() {  
  105.         btn_leftTop = (Button) findViewById(R.id.btn_leftTop);  
  106.         btn_rightTop = (Button) findViewById(R.id.btn_rightTop);  
  107.         tv_head = (TextView) findViewById(R.id.tv_head);  
  108.           
  109.         btn_leftTop.setVisibility(View.INVISIBLE);  
  110.         tv_head.setText("添加考试");  
  111.     }  
  112.       
  113.    @Override  
  114.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  115.         // TODO Auto-generated method stub  
  116.   
  117.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  118.   
  119.             AlertDialog.Builder builder = new AlertDialog.Builder(getParent());  
  120.             builder.setMessage("你确定退出吗?")  
  121.                     .setCancelable(false)  
  122.                     .setPositiveButton("确定",  
  123.                             new DialogInterface.OnClickListener() {  
  124.                                 public void onClick(DialogInterface dialog,  
  125.                                         int id) {  
  126.                                     finish();  
  127.                                     System.exit(0);  
  128.                                 }  
  129.                             })  
  130.                     .setNegativeButton("返回",  
  131.                             new DialogInterface.OnClickListener() {  
  132.                                 public void onClick(DialogInterface dialog,  
  133.                                         int id) {  
  134.                                     dialog.cancel();  
  135.                                 }  
  136.                             });  
  137.             AlertDialog alert = builder.create();  
  138.             alert.show();  
  139.             return true;  
  140.         }  
  141.   
  142.         return super.onKeyDown(keyCode, event);  
  143.     }  
  144. }  
      这里继承了ActivityGroup,没有使用过的朋友从百度搜索下就明白了。

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

    

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值