首先,我们来看看这张图吧

看下面的menu菜单,是原装的菜单,好丑陋哦,类似于小编这么爱美的人来说,纯粹就是天大的打击,接受不起。于是,小编就发奋图强,努力,努力,再努力,终于,将菜单改的漂亮了一点,不信你看看。

看到了吧,面对这么漂亮的menu菜单你怎么会不心动呢?心动不如行动,仔细看看下面我是怎么实现的吧:

新建一布局文件menu_main.xml,源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/widget32"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<LinearLayout
		android:id="@+id/widget33"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:padding="5dp"
		android:background="@drawable/menu_bg"
		android:layout_gravity="center_horizontal">
		<LinearLayout
			android:id="@+id/menu_update_btn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center"
			android:layout_weight="1"
			android:background="@drawable/menu_btn_bg"
			android:layout_gravity="center_horizontal">
			<ImageView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:src="@drawable/btn_update" />
    		<TextView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:text="更新系统"
	    		android:textColor="#eee"/>
		</LinearLayout>
		<LinearLayout
			android:id="@+id/menu_aboutus_btn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center"
			android:layout_weight="1"
			android:background="@drawable/menu_btn_bg"
			android:layout_gravity="center_horizontal">
			<ImageView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:src="@drawable/btn_aboutus" />
    		<TextView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:text="关于我们"
	    		android:textColor="#eee"/>
		</LinearLayout>
		<LinearLayout
			android:id="@+id/menu_systeminf_btn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center"
			android:layout_weight="1"
			android:background="@drawable/menu_btn_bg"
			android:layout_gravity="center_horizontal">
			<ImageView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:src="@drawable/btn_systeminf" />
    		<TextView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:text="系统信息"
	    		android:textColor="#eee"/>
		</LinearLayout>
		<LinearLayout
			android:id="@+id/menu_close_btn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center"
			android:layout_weight="1"
			android:background="@drawable/menu_btn_bg"
			android:layout_gravity="center_horizontal">
			<ImageView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:src="@drawable/btn_close" />
    		<TextView
	    	    android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:text="退出系统"
	    		android:textColor="#eee"/>
		</LinearLayout>
	</LinearLayout>
</LinearLayout>

布局完成之后,在想要出现menu的Activity添加onKeyDown事件,代码为:

public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
    	{  //获取 back键
        	if(menu_display)
        	{
        		//如果 Menu已经打开 ,先关闭Menu
        		menuWindow.dismiss();
        		menu_display = false;
        	}
        	else
        	{
        		Intent intent = new Intent();
            	intent.setClass(MainActivity.this,ExitActivity.class);
            	startActivity(intent);
        	}
    	}

    	else if(keyCode == KeyEvent.KEYCODE_MENU)
    	{   //获取 Menu键
			if(!menu_display)
			{
				//获取LayoutInflater实例
				inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
				//这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
				//该方法返回的是一个View的对象,是布局中的根
				layout = inflater.inflate(R.layout.menu_main, null);

				//下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
				menuWindow = new PopupWindow(layout,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //后两个参数是width和height
				//menuWindow.showAsDropDown(layout); //设置弹出效果
				//menuWindow.showAsDropDown(null, 0, layout.getHeight());
				menuWindow.showAtLocation(this.findViewById(R.id.mainmymusic), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
				//如何获取我们main中的控件呢?也很简单
				mUpdateBtn=(LinearLayout)layout.findViewById(R.id.menu_update_btn);
				mAboutusBtn=(LinearLayout)layout.findViewById(R.id.menu_aboutus_btn);
				mSysteminfBtn=(LinearLayout)layout.findViewById(R.id.menu_systeminf_btn);
				mCloseBtn = (LinearLayout)layout.findViewById(R.id.menu_close_btn);

				//下面对每一个Layout进行单击事件的注册吧。。。
				//比如单击某个MenuItem的时候,他的背景色改变
				//事先准备好一些背景图片或者颜色
				mUpdateBtn.setOnClickListener (new View.OnClickListener() {
					public void onClick(View arg0) {
						Toast.makeText(MainActivity.this, "没有更新的系统版本", Toast.LENGTH_LONG).show();
			        	menuWindow.dismiss(); //响应点击事件之后关闭Menu
					}
				});

				mAboutusBtn.setOnClickListener (new View.OnClickListener() {
					public void onClick(View arg0) {
						Toast.makeText(MainActivity.this, "我们是一个民间组织哦~~", Toast.LENGTH_LONG).show();
			        	menuWindow.dismiss(); //响应点击事件之后关闭Menu
					}
				});

				mSysteminfBtn.setOnClickListener (new View.OnClickListener() {
					public void onClick(View arg0) {
						Toast.makeText(MainActivity.this, "我的随身听音乐", Toast.LENGTH_LONG).show();
			        	menuWindow.dismiss(); //响应点击事件之后关闭Menu
					}
				});

				mCloseBtn.setOnClickListener (new View.OnClickListener() {
					public void onClick(View arg0) {
						//Toast.makeText(Main.this, "退出", Toast.LENGTH_LONG).show();
						Intent intent = new Intent();
			        	intent.setClass(MainActivity.this,ExitActivity.class);
			        	startActivity(intent);
			        	menuWindow.dismiss(); //响应点击事件之后关闭Menu
					}
				});	

				menu_display = true;
			}
			else
			{
				//如果当前已经为显示状态,则隐藏起来
				menuWindow.dismiss();
				menu_display = false;
			}
			return false;
		}
    	return false;
    }

上面,还需要定义几个变量,需要定义的变量为:

private LinearLayout mCloseBtn,mSysteminfBtn,mAboutusBtn,mUpdateBtn;
private boolean menu_display = false;
private PopupWindow menuWindow;
private LayoutInflater inflater;
private View layout;

至此,漂亮的menu出来了,心动了吧?那就赶紧打开自己的工程试试吧……

打造属于自己的安卓menu的更多相关文章

  1. 安卓Menu键的问题

    近期开发中有须要Menu键,结果发现了一个非常尴尬的问题.我的測试机上有Menu键.可是測试平板上没有,队友的測试机上竟然也没有Menu键.这着实有些尴尬... 上网谷歌之后才发现问题所在: 仅仅有在 ...

  2. 安卓menu的介绍与使用

    菜单之前是用户点击系统的菜单键才展示出来的,后来这个键渐渐被移除,菜单变成了点击任意的view都可以展示.菜单非为3种: 1.Options menu and action bar  选项菜单和操作栏 ...

  3. 打造属于自己的安卓Metro界面

    前言: 各位小伙伴,又到了每周更新文章了时候了,本来是周日能发出来呢,这不是赶上清明节吗,女王大人发话了,清明节前两天半陪她玩,只留给我周一下午半天时间写博客,哪里有女王哪里就有压迫呀有木有!好了闲话 ...

  4. 我们来八一八阿里云OS的实质和历史

    有个姓许的朋友在微信公众号上这样评论: 但是楼主对yunos的了解程度有多少,建议去了解下再评价别人,免费给你普及下:http://www.ithome.com/html/digi/109484.ht ...

  5. 支持多种浏览器的纯css下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Google工程师打造Remix OS系统 桌面版安卓下载

    三位前Google工程师打造的Remix OS系统终于来到了PC桌面上,现已可以下载尝鲜. Remix OS for PC基于Android-x86项目,由安卓5.1 Lollipop深度定制而来,不 ...

  7. 初学安卓开发随笔之 Menu、toast 用法、活动的四种启动模式 以及 一个方便的Base活动类使用方法

    Toast toast 是安卓系统的一种非常棒的提醒方式 首先定义一个弹出Toast的触发点,比如可以是按钮之类 其中 Toast.LENGTH_SHORT是指显示时长 还有一个内置变量为Toast. ...

  8. Nginx集群之.Net打造WebApp(支持IOS和安卓)

    目录 1       大概思路... 1 2       Nginx集群之.Net打造WebApp(支持IOS和安卓) 1 3       安卓模拟器... 1 4       MUI框架... 3 ...

  9. 安卓开发学习之Menu

    安卓开发中菜单是一个很重要的组件,从安卓开发文档(http://wear.techbrood.com/guide/index.html)中可以看到,安卓UI设计中的Menu主要分为: A.Option ...

随机推荐

  1. delphi连接sql server的字符串2011-10-11 16:07

    delphi连接sql server的字符串2011-10-11 16:07 一.delphi连接sql server 放一个连接组件 ADOConnection, 其它组件TADODataSet,T ...

  2. 转:oralce常用操作、查询语句(查看表空间)

    http://highill.iteye.com/blog/1534858 最近整理一下oralce的常用语句,借此记录一下,在网上都应该能搜到,这里主要是整理分享. 一.操作语句 建立表空间 MYD ...

  3. Spring mvc 具体RequestMapping 参数含义

    今天遇到碰到有人问我个问题,RequestMapping中参数的意义,哎呀傻眼了,果断查资料,这下知道了. http://blog.csdn.net/kobejayandy/article/detai ...

  4. 记录一次代码错误,elastic search的INDEX需要使用小写字母

    Caused by: org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot determine write shards ...

  5. Libev和LibEvent

    libev和libevent功能基本相同,名称相近,到底该用哪一个呢?zhouhh@zhh64:~$ sudo apt-cache search libeventlibevent-dev – Deve ...

  6. css盒模型不同浏览器下解释不同 解决办法

    盒子模型是css中一个重要的概念,理解了盒子模型才能更好的排版.其实盒子模型有两种,分别是 ie 盒子模型和标准 w3c 盒子模型.他们对盒子模型的解释各不相同,先来看看我们熟知的标准盒子模型: 从上 ...

  7. 20145201 实验二 Java面向对象程序设计

    20145201实验二 Java面向对象程序设计 初步掌握单元测试和TDD 实验步骤 (一)单元测试 (1) 三种代码 编程是智力活动,不是打字,编程前要把干什么.如何干想清楚才能把程序写对.写好.与 ...

  8. java中泛型的一个小问题

    最近做项目,由于java语法不是非常的熟悉,编写代码过程中不难遇到一些问题: 代码里写了一条这种语句: Map<String, List<String>> configFile ...

  9. vs2017创建dotnetcore web项目,并部署到centos7上

    一.打开vs2017创建web项目 二.简单的创建项目后,发布项目 三. 在centos上创建webroot目录,将发布的项目文件复制到该目录下(本人用虚拟机测试) 四.在webroot目录下打开终端 ...

  10. 编程练习赛11B 物品价值(装压dp)

    题意:每个物品有m(m<=10)种属性和一个价格,你有n种物品从中任意选择一些物品,让每种属性恰好有奇数个物品拥有,输出满足条件的最大价值和 题解:一看就是明显的01背包问题,但是价格乘以个数的 ...