android 中自定义菜单

在android开发的过程中系统自带的菜单往往满足不了开发中的一些需求,比如说一排最多只能放置三个菜单,坐多只能放置6个,再多的话就会折叠起来,如果我们想再一排显示4个或5个菜单那么就要自己想办法处理。

这里我用布局的隐藏并加上动画来模拟菜单的效果。

要点:

1、隐藏和显示菜单,我使用了一个线性布局把菜单封装起来。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_alignParentBottom="true"android:background="@drawable/menubackground"android:layout_width="fill_parent"android:layout_height="144px"android:orientation="vertical"android:gravity="center"android:visibility="gone"android:id="@+id/lines"><LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="fill_parent"android:layout_height="72px"><ImageButtonandroid:layout_marginLeft="8dip" android:id="@+id/menu_btn_index"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_index_selector"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_news_selector"android:id="@+id/menu_btn_news"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_lib_selector"android:id="@+id/menu_btn_lib"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_add_selector"android:id="@+id/menu_btn_add"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_set_selector"android:id="@+id/menu_btn_set"/></LinearLayout><LinearLayout android:orientation="horizontal" android:gravity="center"android:layout_width="fill_parent"android:layout_height="72px"><ImageButtonandroid:layout_marginLeft="8dip" android:id="@+id/menu_btn_index"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_index_selector"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_news_selector"android:id="@+id/menu_btn_news"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_lib_selector"android:id="@+id/menu_btn_lib"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_add_selector"android:id="@+id/menu_btn_add"/><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_quit_selector"android:id="@+id/menu_btn_quit"/></LinearLayout>
</LinearLayout>

2、模拟菜单的效果,增加动画,布局显示的时候增加一个渐渐底部生气的效果,隐藏的时候增加一个缓缓下落的效果,显示菜单动画文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="00"android:toYDelta="140"android:duration="200" />
</set>

隐藏菜单动画文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromXDelta="0"android:toXDelta="0"android:fromYDelta="140"android:toYDelta="00"android:duration="200" />
</set>

动画调用:

 /*** 显示菜单栏, 重新实现的Option menu.* */private void showAppMenu() {if (menuShowAnimation == null) {menuShowAnimation = AnimationUtils.loadAnimation(mContext, R.anim.menuhide);}myLayout.startAnimation(menuShowAnimation);myLayout.setVisibility(View.VISIBLE);}/*** 隐藏菜单栏, 重新实现的Option menu.* */private void hideAppMenu() {myLayout.setVisibility(View.GONE);if (menuHideAnimation == null)menuHideAnimation =AnimationUtils.loadAnimation(mContext, R.anim.menushow);myLayout.startAnimation(menuHideAnimation);}

3、控制菜单的隐藏和显示,需要重写三个方法public boolean onCreateOptionsMenu(Menu menu),

public boolean dispatchKeyEvent(KeyEvent event) 和public boolean dispatchTouchEvent(MotionEvent event)

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {if(mCustomMenu==null)mCustomMenu=new CustomMenu(CustomMenuActivity.this,CustomMenuActivity.this);mCustomMenu.CreateMenu();return false;}@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(mCustomMenu!=null)return mCustomMenu.dispatchKeyEvent(event,super.dispatchKeyEvent(event));return super.dispatchKeyEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {if(mCustomMenu!=null)return mCustomMenu.dispatchTouchEvent(event,super.dispatchTouchEvent(event));return super.dispatchTouchEvent(event);}

4、实现菜单点击时候被点击菜单状态的般变化,这里我使用了selector来实现,菜单我使用ImageButton将selector赋值给ImageButton 的background即可:

一个菜单项

<ImageButtonandroid:layout_marginLeft="8dip" android:id="@+id/menu_btn_index"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/menu_index_selector"/>

menu_index_selector 文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_window_focused="false"android:drawable="@drawable/menu_index" /><item android:state_pressed="true"android:drawable="@drawable/menu_index1" /><item android:state_focused="true"android:drawable="@drawable/menu_index1" /><itemandroid:drawable="@drawable/menu_index" />
</selector>

 

5、页面的调用使用:<include>标签来进行引用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><include layout="@layout/menu_layout"/></RelativeLayout>

这样的话一个模拟的自定义菜单就基本完成了,菜单控制完整代码java类:

package com.demo.utils;import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;import com.demo.HelloWorld.R;/*** @author Administrator*         xsl  xushilin@kingtoneinfo.com* @version: 创建时间:2011-8-30 上午11:16:19* 说         明:* 修改历史:*/
public class CustomMenu {private LinearLayout myLayout;private  Context mContext;private Activity mActivity;private Animation menuShowAnimation = null;private Animation menuHideAnimation = null;private Resources res;public int screen_height;private ImageButton imgIndex,imgSet,imgNews,imgAdd,imgQuit,imgLib;   public CustomMenu(Context context,Activity activity){mContext=context; mActivity=activity;res = mContext.getResources();screen_height = res.getDisplayMetrics().heightPixels;myLayout=(LinearLayout)activity.findViewById(R.id.lines);imgIndex=(ImageButton)activity.findViewById(R.id.menu_btn_index);   imgSet=(ImageButton)activity.findViewById(R.id.menu_btn_set);imgNews=(ImageButton)activity.findViewById(R.id.menu_btn_news);imgAdd=(ImageButton)activity.findViewById(R.id.menu_btn_add);imgQuit=(ImageButton)activity.findViewById(R.id.menu_btn_quit);imgLib=(ImageButton)activity.findViewById(R.id.menu_btn_lib);//返回首页imgIndex.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}          });//设置imgSet.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}         });//查询imgNews.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}            });//编辑imgAdd.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}         });//退出系统imgQuit.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}          });//素材库imgLib.setOnClickListener(new OnClickListener(){public void onClick(View v) {//TODO do somthing}            });}public  void  CreateMenu(){     if(myLayout.getVisibility()==View.GONE)showAppMenu();//myLayout.setVisibility(View.VISIBLE);      elsehideAppMenu();//myLayout.setVisibility(View.GONE);}/*** 显示菜单栏, 重新实现的Option menu.* */private void showAppMenu() {if (menuShowAnimation == null) {menuShowAnimation = AnimationUtils.loadAnimation(mContext, R.anim.menuhide);}myLayout.startAnimation(menuShowAnimation);myLayout.setVisibility(View.VISIBLE);}/*** 隐藏菜单栏, 重新实现的Option menu.* */private void hideAppMenu() {myLayout.setVisibility(View.GONE);if (menuHideAnimation == null)menuHideAnimation =AnimationUtils.loadAnimation(mContext, R.anim.menushow);myLayout.startAnimation(menuHideAnimation);}public boolean dispatchTouchEvent(MotionEvent event,boolean b) {if (myLayout.getVisibility() == View.VISIBLE) {int y = (int) event.getRawY();if (y < screen_height - myLayout.getHeight()) {hideAppMenu();return true;}}      return b;}public boolean dispatchKeyEvent(KeyEvent event,boolean b) {int act = event.getAction();int code = event.getKeyCode();           // app menu like option menuif (code == KeyEvent.KEYCODE_MENU){if (act == KeyEvent.ACTION_DOWN){if (myLayout.getVisibility() == View.VISIBLE) {hideAppMenu();} else {showAppMenu();}return true;}}else if (code == KeyEvent.KEYCODE_BACK){if (myLayout.getVisibility() == View.VISIBLE) {hideAppMenu();return true;}}return b;    }
}

activity调用菜单完整代码:

package com.demo.ui;import com.demo.HelloWorld.R;
import com.demo.utils.CustomMenu;import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;/*** @author XSL*         xsl  xushilin@kingtoneinfo.com* @version: 创建时间:2011-8-30 上午11:13:14* 说         明:* 修改历史:*/
public class CustomMenuActivity extends Activity {private CustomMenu mCustomMenu=null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.custom_menu);      }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {if(mCustomMenu==null)mCustomMenu=new CustomMenu(CustomMenuActivity.this,CustomMenuActivity.this);mCustomMenu.CreateMenu();return false;}@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(mCustomMenu!=null)return mCustomMenu.dispatchKeyEvent(event,super.dispatchKeyEvent(event));return super.dispatchKeyEvent(event);}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {if(mCustomMenu!=null)return mCustomMenu.dispatchTouchEvent(event,super.dispatchTouchEvent(event));return super.dispatchTouchEvent(event);}}

实现的效果如下:

作者:蓝之风      出处:http://www.cnblogs.com/vaiyanzi/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/zhengbeibei/archive/2012/12/24/2831285.html

Android--android 中自定义菜单相关推荐

  1. android项目中自定义顶部标题栏,Android项目中自定义顶部标题栏

    Android项目中自定义顶部标题栏 下面给大家详细介绍android中自定义顶部标题栏的思路及实现方式 先来图: 思路及实现步骤 1.定义标题栏布局 2.自定义TitleActivity控制标题栏按 ...

  2. Android 平板中 自定义键盘(popuwindow) 居于屏幕左下方 仿微信的密码输入界面

    之前博客中,介绍过使用谷歌提供的键盘的一些api,可以很好地自定义键盘,参考我之前的博客链接:android 自定义键盘 ,这个有一个局限性,只能占满屏幕,无法做到只能占一部分的需求键盘,如下图我平板 ...

  3. Android系统中自定义按键的短按、双击、长按事件

    在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...

  4. 如何自定义火狐背景_在Firefox中自定义菜单

    如何自定义火狐背景 Would you like a way to edit menus and remove the extra entries that you do not use or nee ...

  5. android 自定义菜单开发,Android开发学习笔记:浅谈3大类菜单

    在Android系统中,菜单可以分为三类:选项菜单(Option Menu),上下文菜单(Context Menu)以及子菜单(Sub Menu). 一.选项菜单(Option Menu) 创建选项菜 ...

  6. android studio中使用github (上集)

    android studio 使用github(下集) 1.配置github账号 1.1 进入File->Settings->Version Control->GitHub中,选择下 ...

  7. 微信开发学习总结(四)——自定义菜单(5)——个性化菜单接口

    一.个性化菜单接口说明 为了帮助公众号实现灵活的业务运营,微信公众平台新增了个性化菜单接口,开发者可以通过该接口,让公众号的不同用户群体看到不一样的自定义菜单.该接口开放给已认证订阅号和已认证服务号. ...

  8. Android ActionBar中Overflow Menu(溢出菜单)中的一些问题

    关注微信号:javalearns   随时随地学Java 或扫一扫 随时随地学Java 前言 开始前我们先来关注一下Android Overflow menu的几个相关问题: 什么是Overflow ...

  9. 墨迹天气php,Android_仿墨迹天气在Android App中实现自定义zip皮肤更换,在这里谈一下墨迹天气的换肤 - phpStudy...

    仿墨迹天气在Android App中实现自定义zip皮肤更换 在这里谈一下墨迹天气的换肤实现方式,不过首先声明我只是通过反编译以及参考了一些网上其他资料的方式推测出的换肤原理, 在这里只供参考. 若大 ...

  10. android 自定义声音,如何在Android设备中添加自己的自定义声音

    大多数人在获得新手机后要做的第一件事就是更改铃声.根据制造商的不同,更改警报音,消息音和铃声的选项也有所不同. 如果您不喜欢预装的铃声,则可以根据需要从计算机上更改它.如果您已经将喜欢的铃声存储在计算 ...

最新文章

  1. MySQL基础篇:单行函数
  2. SELECT-OPTIONS对象
  3. 解决Ubuntu14.04安装Chrome浏览器打不开的问题
  4. 装上后这 14 个插件后,PyCharm 能飞起
  5. 删除所有的视图,存储过程
  6. 蓝绿发布、滚动发布、灰度发布,有什么区别?
  7. Supersocket 如何使用 教程1
  8. elm的 java包_README.md
  9. 【AcWing】103. 电影(离散化)
  10. Atitit 数据表 资料整理 常见希腊罗马北欧神话神仙与中国对照表 目录1. 神仙体系 12. 神仙分类 13. 印度大神top10 23.1. 神仙列表约70个大神 21.神仙体
  11. CheckBoxList 只能选2个选项
  12. MergeSort(合并排序)
  13. springboot的测试类
  14. 加载webView使用框架AgenWeb
  15. python产生0101_GitHub - zhl0101/Python-100-Days: Python - 100天从新手到大师
  16. 如何将m3u8格式转成MP4以及可播放格式
  17. 2020年汽车驾驶员(中级)模拟考试题及汽车驾驶员(中级)考试软件
  18. Oracle数据库Bitand()函数用法(计算位移)
  19. Android CardView使用详解
  20. 郑州轻工业大学软件学院计算机网络期末复习

热门文章

  1. EMNLP 2021 | 罗氏和博阿齐奇大学研究合作团队提出:多标签文本分类中长尾分布的平衡策略...
  2. 神经网络十大学习率衰减提效策略!
  3. word2vec理论与实践
  4. 20191127_朴素贝叶斯多分类
  5. 双非院校,0项目经验,三个月入职大厂NLP算法岗,月薪30k+
  6. 一位算法工程师从30+场秋招面试中总结出的超强面经—文本检测与GAN篇(含答案)...
  7. 机器学习基础算法30-贝叶斯网络理论
  8. PLA算法(感知机)
  9. 图论算法——有向图的邻接链表实现
  10. CIA网攻中国11年,内网防护刻不容缓!