本文实例讲述了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)
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:// * @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程序设计有所帮助。