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

作者:wangkuifeng0118 时间:2022-07-14 10:20:51 

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

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

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

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

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

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


<?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,网易,导航栏
0
投稿

猜你喜欢

  • 通过实例解析java过滤器和拦截器的区别

    2022-12-21 05:02:45
  • Spring Data JPA 实体类中常用注解说明

    2021-07-21 10:39:38
  • Java TCP网络通信协议详细讲解

    2022-09-26 09:04:22
  • SpringBoot搭配AOP实现自定义注解

    2022-04-07 01:21:59
  • java8 利用reduce实现将列表中的多个元素的属性求和并返回操作

    2021-09-29 06:53:38
  • WPF使用Geometry绘制几何图形

    2023-08-14 14:13:31
  • Android自定义星星可滑动评分控件

    2022-03-18 10:23:03
  • spring boot线上日志级别动态调整的配置步骤

    2022-09-19 01:57:45
  • MyBatis SpringMVC整合实现步骤详解

    2023-02-02 20:33:07
  • Java设计模式开发中使用观察者模式的实例教程

    2021-06-22 15:54:19
  • Spring Validation方法实现原理分析

    2023-09-04 17:11:55
  • Android仿支付宝支付从底部弹窗效果

    2022-04-30 10:37:13
  • 为何Linq的Distinct实在是不给力

    2023-05-11 17:03:18
  • AndroidStudio报错Emulator:PANIC:Cannot find AVD system path. Please define ANDROID_SDK_ROOT(解决方案)

    2023-11-27 01:33:28
  • C#实现输入10个数存入到数组中并求max和min及平均数的方法示例

    2023-11-30 05:58:54
  • Unity3D启动外部程序并传递参数的实现

    2021-12-10 07:10:59
  • 关于C# dynamic装箱问题

    2021-09-13 19:22:40
  • Java获得当前时间前指定几个小时具体时间的方法示例

    2023-11-24 04:56:49
  • Android 调用发送短信的方法

    2023-05-30 22:56:40
  • java反射之方法反射的基本操作方法

    2021-11-26 00:45:36
  • asp之家 软件编程 m.aspxhome.com