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程序设计有所帮助。