Android编程自定义菜单实现方法详解

 更新时间:2017年02月22日 14:23:01   作者:蓝之风  
这篇文章主要介绍了Android编程自定义菜单实现方法,结合实例形式分析了Android自定义菜单的布局、动画及功能相关实现技巧与注意事项,需要的朋友可以参考下

本文实例讲述了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"
   >
 <ImageButton
   android: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"
   />
  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_news_selector"
   android:id="@+id/menu_btn_news"
   />
  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_lib_selector"
   android:id="@+id/menu_btn_lib"
   />
    <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_add_selector"
   android:id="@+id/menu_btn_add"
   />
 <ImageButton
   android: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">
 <ImageButton
   android: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"
   />
  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_news_selector"
   android:id="@+id/menu_btn_news"
   />
  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_lib_selector"
   android:id="@+id/menu_btn_lib"
   />
  <ImageButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/menu_add_selector"
   android:id="@+id/menu_btn_add"
   />
 <ImageButton
   android: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">
  <translate
    android: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">
  <translate
    android: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)

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

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

一个菜单项

<ImageButton
   android: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 Project
  Licensed 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 at
   http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed 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 and
  limitations 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" />
 <item
   android:drawable="@drawable/menu_index" />
</selector>

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns: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);
    else
      hideAppMenu();
      //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 menu
   if (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;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_menu);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    if(mCustomMenu==null)
      mCustomMenu=new CustomMenu(CustomMenuActivity.this,CustomMenuActivity.this);
    mCustomMenu.CreateMenu();
    return false;
  }
  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    if(mCustomMenu!=null)
      return mCustomMenu.dispatchKeyEvent(event,super.dispatchKeyEvent(event));
    return super.dispatchKeyEvent(event);
  }
  @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    if(mCustomMenu!=null)
      return mCustomMenu.dispatchTouchEvent(event,super.dispatchTouchEvent(event));
    return super.dispatchTouchEvent(event);
  }
}

实现的效果如下:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android ListView之setEmptyView正确使用方法

    Android ListView之setEmptyView正确使用方法

    这篇文章主要介绍了Android ListView之setEmptyView正确使用方法的相关资料,希望通过本文能帮助到大家使用该方法,需要的朋友可以参考下
    2017-09-09
  • 利用Jetpack Compose实现经典俄罗斯方块游戏

    利用Jetpack Compose实现经典俄罗斯方块游戏

    你的童年是否有俄罗斯方块呢,本文就来介绍如何通过Jetpack Compose实现一个俄罗斯方块!感兴趣的小伙伴快跟随小编一起动手尝试一下吧
    2022-05-05
  • Android Gradle多渠道打包的实现方法

    Android Gradle多渠道打包的实现方法

    这篇文章主要介绍了Android Gradle多渠道打包的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Android SQLite数据库连接实现登录功能

    Android SQLite数据库连接实现登录功能

    这篇文章主要为大家详细介绍了Android SQLite数据库连接实现登录功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • Android仿抖音上下滑动布局

    Android仿抖音上下滑动布局

    这篇文章主要为大家详细介绍了Android仿抖音上下滑动布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • Android通过手势实现答题器翻页效果

    Android通过手势实现答题器翻页效果

    这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android ListView自定义Adapter实现仿QQ界面

    Android ListView自定义Adapter实现仿QQ界面

    这篇文章主要为大家详细介绍了ListView自定义Adapter实现仿QQ界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android Retrofit使用详细教程

    Android Retrofit使用详细教程

    Retrofit是Android用来接口请求的网络框架,内部是基于OkHttp实现的,retrofit负责接口请求的封装,retrofit可以直接将接口数据解析为Bean类、List集合等,直接简化了中间繁琐的数据解析过程,这篇文章主要介绍了Android Retrofit使用详情,需要的朋友可以参考下
    2024-03-03
  • android顶部(toolbar)搜索框实现代码

    android顶部(toolbar)搜索框实现代码

    这篇文章主要介绍了android顶部(toolbar)搜索框实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • Flutter学习之构建、布局及绘制三部曲

    Flutter学习之构建、布局及绘制三部曲

    这篇文章主要给大家介绍了关于Flutter学习之构建、布局及绘制三部曲的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04

最新评论