Android实现扩展Menu的方法
程序员文章站
2023-12-01 19:44:46
本文实例讲述了android实现扩展menu的方法。分享给大家供大家参考。具体如下:
1. java代码:
package com.tabmenu;
impo...
本文实例讲述了android实现扩展menu的方法。分享给大家供大家参考。具体如下:
1. java代码:
package com.tabmenu; import android.content.context; import android.graphics.color; import android.graphics.drawable.colordrawable; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import android.widget.linearlayout; import android.widget.popupwindow; import android.widget.textview; import android.widget.adapterview.onitemclicklistener; import android.widget.linearlayout.layoutparams; /**可扩展menu * * @author antking **/ public class tabmenu extends popupwindow{ private gridview gvbody, gvtitle; private linearlayout mlayout; private menutitleadapter titleadapter; public tabmenu(context context,onitemclicklistener titleclick,onitemclicklistener bodyclick, menutitleadapter titleadapter,int colorbgtabmenu,int anitabmenu){ super(context); mlayout = new linearlayout(context); mlayout.setorientation(linearlayout.vertical); //标题选项栏 gvtitle = new gridview(context); gvtitle.setlayoutparams(new layoutparams(layoutparams.fill_parent, layoutparams.wrap_content)); gvtitle.setnumcolumns(titleadapter.getcount()); gvtitle.setstretchmode(gridview.stretch_column_width); gvtitle.setverticalspacing(1); gvtitle.sethorizontalspacing(1); gvtitle.setgravity(gravity.center); gvtitle.setonitemclicklistener(titleclick); gvtitle.setadapter(titleadapter); gvtitle.setselector(new colordrawable(color.transparent));//选中的时候为透明色 this.titleadapter=titleadapter; //子选项栏 gvbody = new gridview(context); gvbody.setlayoutparams(new layoutparams(layoutparams.fill_parent,layoutparams.wrap_content)); gvbody.setselector(new colordrawable(color.transparent));//选中的时候为透明色 gvbody.setnumcolumns(4); gvbody.setstretchmode(gridview.stretch_column_width); gvbody.setverticalspacing(10); gvbody.sethorizontalspacing(10); gvbody.setpadding(10, 10, 10, 10); gvbody.setgravity(gravity.center); gvbody.setonitemclicklistener(bodyclick); mlayout.addview(gvtitle); mlayout.addview(gvbody); //设置默认项 this.setcontentview(mlayout); this.setwidth(layoutparams.fill_parent); this.setheight(layoutparams.wrap_content); this.setbackgrounddrawable(new colordrawable(colorbgtabmenu));// 设置tabmenu菜单背景 this.setanimationstyle(anitabmenu); this.setfocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应 } public void settitleselect(int index) { gvtitle.setselection(index); this.titleadapter.setfocus(index); } public void setbodyselect(int index,int colorselbody) { int count=gvbody.getchildcount(); for(int i=0;i<count;i++) { if(i!=index) ((linearlayout)gvbody.getchildat(i)).setbackgroundcolor(color.transparent); } ((linearlayout)gvbody.getchildat(index)).setbackgroundcolor(colorselbody); } public void setbodyadapter(menubodyadapter bodyadapter) { gvbody.setadapter(bodyadapter); } /** * 自定义adapter,tabmenu的每个分页的主体 * */ static public class menubodyadapter extends baseadapter { private context mcontext; private int fontcolor,fontsize; private string[] texts; private int[] resid; /** * 设置tabmenu的分页主体 * @param context 调用方的上下文 * @param texts 按钮集合的字符串数组 * @param resid 按钮集合的图标资源数组 * @param fontsize 按钮字体大小 * @param color 按钮字体颜色 */ public menubodyadapter(context context, string[] texts,int[] resid, int fontsize,int fontcolor) { this.mcontext = context; this.fontcolor = fontcolor; this.texts = texts; this.fontsize=fontsize; this.resid=resid; } public int getcount() { return texts.length; } public object getitem(int position) { return makemenybody(position); } public long getitemid(int position) { return position; } private linearlayout makemenybody(int position) { linearlayout result=new linearlayout(this.mcontext); result.setorientation(linearlayout.vertical); result.setgravity(gravity.center_horizontal|gravity.center_vertical); result.setpadding(10, 10, 10, 10); textview text = new textview(this.mcontext); text.settext(texts[position]); text.settextsize(fontsize); text.settextcolor(fontcolor); text.setgravity(gravity.center); text.setpadding(5, 5, 5, 5); imageview img=new imageview(this.mcontext); img.setbackgroundresource(resid[position]); result.addview(img,new linearlayout.layoutparams(new layoutparams(layoutparams.wrap_content,layoutparams.wrap_content))); result.addview(text); return result; } public view getview(int position, view convertview, viewgroup parent) { return makemenybody(position); } } /** * 自定义adapter,tabmenu的分页标签部分 * */ static public class menutitleadapter extends baseadapter { private context mcontext; private int fontcolor,unselcolor,selcolor; private textview[] title; /** * 设置tabmenu的title * @param context 调用方的上下文 * @param titles 分页标签的字符串数组 * @param fontsize 字体大小 * @param fontcolor 字体颜色 * @param unselcolor 未选中项的背景色 * @param selcolor 选中项的背景色 */ public menutitleadapter(context context, string[] titles, int fontsize, int fontcolor,int unselcolor,int selcolor) { this.mcontext = context; this.fontcolor = fontcolor; this.unselcolor = unselcolor; this.selcolor=selcolor; this.title = new textview[titles.length]; for (int i = 0; i < titles.length; i++) { title[i] = new textview(mcontext); title[i].settext(titles[i]); title[i].settextsize(fontsize); title[i].settextcolor(fontcolor); title[i].setgravity(gravity.center); title[i].setpadding(10, 10, 10, 10); } } public int getcount() { return title.length; } public object getitem(int position) { return title[position]; } public long getitemid(int position) { return title[position].getid(); } /** * 设置选中的效果 */ private void setfocus(int index) { for(int i=0;i<title.length;i++) { if(i!=index) { title[i].setbackgrounddrawable(new colordrawable(unselcolor));//设置没选中的颜色 title[i].settextcolor(fontcolor);//设置没选中项的字体颜色 } } title[index].setbackgroundcolor(0x00);//设置选中项的颜色 title[index].settextcolor(selcolor);//设置选中项的字体颜色 } public view getview(int position, view convertview, viewgroup parent) { view v; if (convertview == null) { v = title[position]; } else { v = convertview; } return v; } } }
2. java代码:
package com.tabmenu; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.view.gravity; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.toast; /** * 活动界面 * @author wei * */ public class activity01 extends activity { tabmenu.menubodyadapter[] bodyadapter=new tabmenu.menubodyadapter[3]; tabmenu.menutitleadapter titleadapter; tabmenu tabmenu; int seltitle=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); //设置分页栏的标题 titleadapter = new tabmenu.menutitleadapter(this, new string[] { "常用", "设置", "工具" }, 16, 0xff222222,color.ltgray,color.white); //定义每项分页栏的内容 bodyadapter[0]=new tabmenu.menubodyadapter(this,new string[] { "常用1", "常用2", }, new int[] { android.r.drawable.alert_dark_frame, android.r.drawable.ic_delete},13, 0xffffffff); bodyadapter[1]=new tabmenu.menubodyadapter(this,new string[] { "设置1", "设置2", "设置3"}, new int[] { android.r.drawable.ic_menu_edit, android.r.drawable.btn_default,android.r.drawable.btn_dropdown},13, 0xffffffff); bodyadapter[2]=new tabmenu.menubodyadapter(this,new string[] { "工具1", "工具2", "工具3", "工具4" }, new int[] { android.r.drawable.ic_media_ff, android.r.drawable.ic_menu_delete, android.r.drawable.ic_btn_speak_now, android.r.drawable.edit_text },13, 0xffffffff); tabmenu=new tabmenu(this, new titleclickevent(), new bodyclickevent(), titleadapter, 0x55123456,//tabmenu的背景颜色 r.style.popupanimation);//出现与消失的动画 tabmenu.update(); tabmenu.settitleselect(0); tabmenu.setbodyadapter(bodyadapter[0]); } class titleclickevent implements onitemclicklistener{ @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { seltitle=arg2; tabmenu.settitleselect(arg2); tabmenu.setbodyadapter(bodyadapter[arg2]); } } class bodyclickevent implements onitemclicklistener{ @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { tabmenu.setbodyselect(arg2,color.gray); string str="第"+string.valueof(seltitle)+"栏/n/r" +"第"+string.valueof(arg2)+"项"; toast.maketext(activity01.this, str, 500).show(); } } @override /** * 创建menu */ public boolean oncreateoptionsmenu(menu menu) { menu.add("menu");// 必须创建一项 return super.oncreateoptionsmenu(menu); } @override /** * 拦截menu */ public boolean onmenuopened(int featureid, menu menu) { if (tabmenu != null) { if (tabmenu.isshowing()) tabmenu.dismiss(); else { tabmenu.showatlocation(findviewbyid(r.id.linearlayout01), gravity.bottom, 0, 0); } } return false;// 返回为true 则显示系统menu } }
3. 运行结果:
希望本文所述对大家的android程序设计有所帮助。