在使用uc-webbrowser时,你会发现它的弹出菜单跟系统自带的菜单不一样。它实现更多菜单选项的显示和分栏。其实,它的本身是popupwindow或者是alertdialog对话框,在里面添加两个gridview控件,一个是菜单标题栏,一个是菜单选项。菜单选项视图的切换可以通过适配器的变换,轻松地实现。
一、运行截图:
二、实现要点:
(1)屏蔽系统弹出的菜单:
1、首先创建至少一个系统的菜单选项
复制代码 代码如下:
@override
public boolean oncreateoptionsmenu(menu menu) {
menu.add("menu");
return super.oncreateoptionsmenu(menu);
}
2、在onmenuopened方法里显示自己的菜单视图,并返回false。
复制代码 代码如下:
@override
public boolean onmenuopened(int featureid, menu menu) {
mymenu.showatlocation(findviewbyid(r.id.layout), gravity.bottom, 0,0);
return false; // true--显示系统自带菜单;false--不显示。
}
(2)点击菜单栏,切换菜单视图时,只要重新设置当前的适配器对象就可以。
复制代码 代码如下:
gv_body.setadapter(bodyadapter[arg2]); //改变选项视图
(3)继承popupwindow,重写一个类实现弹出对话框,主要是为了更好更简便实现弹出菜单的样式和事件响应等等。
复制代码 代码如下:
public class mydefinedmenu extends popupwindow { 。。。}
三、 具体代码如下:
(1)布局:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</linearlayout>
(2)程序代码
1、主类:mymenu
复制代码 代码如下:
package com.myandroid.test;
import java.util.arraylist;
import java.util.list;
import android.app.activity;
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;
public class mymenu extends activity {
private list<string> titles; //标题栏
private list<list<string>> item_names; //选项名称
private list<list<integer>> item_images; //选项图标
private mydefinedmenu mymenu; //弹出菜单
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
//弹出菜单标题栏
titles = additems(new string[]{"菜单一", "菜单二", "菜单三"});
//选项图标
item_images = new arraylist<list<integer>>();
item_images.add(additems(new integer[]{r.drawable.bag,
r.drawable.bluetooth, r.drawable.earth, r.drawable.email}));
item_images.add(additems(new integer[]{r.drawable.map,
r.drawable.news, r.drawable.reader, r.drawable.sound, r.drawable.tape}));
item_images.add( additems(new integer[]{r.drawable.telephone,
r.drawable.bluetooth, r.drawable.earth, r.drawable.email}));
//选项名称
item_names = new arraylist<list<string>>();
item_names.add(additems(new string[]{"购物", "蓝牙", "游览器", "邮件"}));
item_names.add(additems(new string[]{"地图", "新闻", "阅读器", "音箱", "录音"}));
item_names.add(additems(new string[]{"电话", "蓝牙", "阅读器", "邮箱"}));
//创建弹出菜单对象
mymenu = new mydefinedmenu(this, titles, item_names,
item_images, new itemclickevent());
}
/**
* 转换为list<string>
* @param values
* @return
*/
private list<string> additems(string[] values) {
list<string> list = new arraylist<string>();
for (string var : values) {
list.add(var);
}
return list;
}
/**
* 转换为list<integer>
* @param values
* @return
*/
private list<integer> additems(integer[] values) {
list<integer> list = new arraylist<integer>();
for (integer var : values) {
list.add(var);
}
return list;
}
@override
public boolean oncreateoptionsmenu(menu menu) {
menu.add("menu");
return super.oncreateoptionsmenu(menu);
}
@override
public boolean onmenuopened(int featureid, menu menu) {
mymenu.showatlocation(findviewbyid(r.id.layout), gravity.bottom, 0,0);
return false; // true--显示系统自带菜单;false--不显示。
}
/**
* 菜单选项点击事件
* @author kobi
*
*/
class itemclickevent implements onitemclicklistener {
@override
public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
long arg3) {
//显示点击的是哪个菜单哪个选项。
toast.maketext(mymenu.this, "menu: " +
titles.get(mymenu.gettitleindex()) +
" item: " + item_names.get(mymenu.gettitleindex()).get(arg2),
toast.length_short).show();
mymenu.dismiss(); //菜单消失
}
}
}
2、弹出菜单类:mydefinedmenu、
复制代码 代码如下:
package com.myandroid.test;
import java.util.list;
import com.myandroid.test.mymenu.itemclickevent;
import android.content.context;
import android.graphics.color;
import android.util.log;
import android.view.view;
import android.view.viewgroup.layoutparams;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.gridview;
import android.widget.linearlayout;
import android.widget.popupwindow;
public class mydefinedmenu extends popupwindow {
private linearlayout layout; //总的布局
private gridview gv_title; //菜单栏
private gridview gv_body; //选项视图
private bodyadatper[] bodyadapter; //选项适配器
private titleadatper titleadapter; //标题适配器
private context context; //上下文
private int titleindex; //菜单序号
public mydefinedmenu(context context, list<string> titles,
list<list<string>> item_names, list<list<integer>> item_images,
itemclickevent itemclickevent) {
super(context);
this.context = context;
//布局框架
layout = new linearlayout(context);
layout.setorientation(linearlayout.vertical);
layout.setlayoutparams(new layoutparams(
layoutparams.fill_parent, layoutparams.wrap_content));
//菜单栏
titleindex = 0;
gv_title = new gridview(context);
titleadapter = new titleadatper(context, titles);
gv_title.setadapter(titleadapter);
gv_title.setlayoutparams(new layoutparams(
layoutparams.fill_parent, layoutparams.wrap_content));
gv_title.setnumcolumns(titles.size()); //菜单个数
gv_title.setbackgroundcolor(color.white);
//选项视图
bodyadapter = new bodyadatper[item_names.size()]; //各个视图适配器
for (int i = 0; i < item_names.size(); i++) {
bodyadapter[i] = new bodyadatper(context, item_names.get(i), item_images.get(i));
}
gv_body = new gridview(context);
gv_body.setnumcolumns(4); //每行显示4个选项
gv_body.setbackgroundcolor(color.transparent);
gv_body.setadapter(bodyadapter[0]); //设置适配器
//菜单项切换
gv_title.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view arg1, int arg2,
long arg3) {
titleindex = arg2; //记录当前选中菜单项序号
titleadapter.setfocus(arg2);
gv_body.setadapter(bodyadapter[arg2]); //改变选项视图
}
});
//设置选项点击事件
gv_body.setonitemclicklistener(itemclickevent);
//添加标题栏和选项
layout.addview(gv_title);
layout.addview(gv_body);
// 添加菜单视图
this.setcontentview(layout);
this.setwidth(layoutparams.fill_parent);
this.setheight(layoutparams.wrap_content);
this.setfocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
}
/**
* 获取当前选中菜单项
* @return 菜单项序号
*/
public int gettitleindex() {
return titleindex;
}
}
3、菜单栏适配器:titleadatper
复制代码 代码如下:
package com.myandroid.test;
import java.util.list;
import android.content.context;
import android.graphics.color;
import android.view.gravity;
import android.view.view;
import android.view.viewgroup;
import android.view.viewgroup.layoutparams;
import android.widget.baseadapter;
import android.widget.gridview;
import android.widget.textview;
public class titleadatper extends baseadapter {
private list<string> titles;
private context context;
private final textview[] tv_titels;
public titleadatper(context context, list<string> titles) {
this.context = context;
this.titles = titles;
tv_titels = new textview[titles.size()];
}
@override
public int getcount() {
// todo auto-generated method stub
return titles.size();
}
@override
public object getitem(int position) {
// todo auto-generated method stub
return position;
}
@override
public long getitemid(int position) {
// todo auto-generated method stub
return position;
}
/**
* 选中后,改变菜单颜色。
* @param position
*/
public void setfocus(int position) {
for (int i = 0; i < titles.size(); i++) {
tv_titels[i].setbackgroundcolor(color.white);
}
tv_titels[position].setbackgroundcolor(color.blue);
}
@override
public view getview(int position, view convertview, viewgroup parent) {
//菜单栏文字项
tv_titels[position] = new textview(context);
tv_titels[position].setgravity(gravity.center);
tv_titels[position].settext(titles.get(position));
tv_titels[position].settextsize(18);
tv_titels[position].setlayoutparams(new gridview.layoutparams(
layoutparams.fill_parent, layoutparams.wrap_content));
return tv_titels[position];
}
}
4、菜单项视图适配器:bodyadatper
复制代码 代码如下:
package com.myandroid.test;
import java.util.list;
import android.content.context;
import android.view.gravity;
import android.view.view;
import android.view.viewgroup;
import android.view.viewgroup.layoutparams;
import android.widget.baseadapter;
import android.widget.gridview;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.textview;
public class bodyadatper extends baseadapter {
private list<string> item_names;
private list<integer> item_images;
private context context;
public bodyadatper(context context, list<string> item_names,
list<integer> item_images) {
this.context = context;
this.item_names = item_names;
this.item_images = item_images;
}
@override
public int getcount() {
// todo auto-generated method stub
return item_images.size();
}
@override
public object getitem(int position) {
// todo auto-generated method stub
return position;
}
@override
public long getitemid(int position) {
// todo auto-generated method stub
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
//总布局
linearlayout layout = new linearlayout(context);
layout.setorientation(linearlayout.vertical);
layout.setgravity(gravity.center);
//选项名称
textview tv_item = new textview(context);
tv_item.setgravity(gravity.center);
tv_item.setlayoutparams(new gridview.layoutparams(
layoutparams.fill_parent, layoutparams.wrap_content));
tv_item.settext(item_names.get(position));
//选项图表
imageview img_item = new imageview(context);
img_item.setlayoutparams(new layoutparams(50, 50));
img_item.setimageresource(item_images.get(position));
//添加选项图标和名字
layout.addview(img_item);
layout.addview(tv_item);
return layout;
}
}
这里是用popupwindow实现,当然也可以用alertdialog或者其他自定义对话框等等,也可以改写menu,还可以用tab实现。实现的方法很多,但原理是相同的,例如用两个gridview,一个作为菜单栏,一个作为菜单项视图。