在溢出菜单自定义菜单项 [英] Custom Menu item in Overflow menu

查看:345
本文介绍了在溢出菜单自定义菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该

如何创建一个自定义菜单项,在附加的图片(谷歌浏览器的应用程序的截图)菜单的第一行酷似在这里输入的形象描述

我曾尝试下面的code在 onCreateOptionsMenu ,其中 R.layout.menu_top_row 是布局第一个row.But只有一个空白行出现在我的情况?我能够显示的选项休息,但无法显示第一行。

 查看孩子= getLayoutInflater()膨胀(R.layout.menu_top_row,NULL);
    菜单项菜单项= menu.add();
    menuItem.setActionView(小孩);


解决方案

从这个堆栈溢出的答案摘自

一个 PopupMenu的是为显示的菜单以及是不是真的有自定义菜单项的外观的好方法。如果你想要的东西更灵活,你的答案是 ListPopupWindow

 私有静态最后弦乐TITLE =称号;
私有静态最后弦乐ICON =图标;私人列表<&HashMap的LT;弦乐,对象>>数据=新的ArrayList<&HashMap的LT;弦乐,对象>>();//使用此项目添加到该ListPopupWindow将使用列表
私人无效的addItem(字符串名称,INT iconResourceId){
    HashMap的<弦乐,对象>地图=新的HashMap<弦乐,对象>();
    map.put(标题,标题);
    map.put(ICON,iconResourceId);
    data.add(地图);
}//当你想显示ListPopupWindow调用此
私人无效showListMenu(查看锚){
    ListPopupWindow popupWindow =新ListPopupWindow(本);    ListAdapter适配器=新SimpleAdapter(
            本,
            数据,
            android.R.layout.activity_list_item,//你可能需要使用你自己的凉爽的布局
            新的String [] {标题,图标} //这些都只是按键的数据用途
            新的INT [] {android.R.id.text1,android.R.id.icon}); //视图IDS的数据映射
    popupWindow.setAnchorView(锚);
    popupWindow.setAdapter(适配器);
    popupWindow.setWidth(400); //注意:不要使用像素,使用扪资源
    popupWindow.setOnItemClickListener(myListener的); //回调时选择了列表项的
    popupWindow.show();
}

How should i make a Custom MenuItem, exactly like the first row in the menu of the attached picture ( screenshot of Google Chrome app)

I have tried the below code in onCreateOptionsMenu, where R.layout.menu_top_row is the layout for the first row.But only a blank row comes up in my case? I am able to display rest of the options, but unable to display the first row.

View child = getLayoutInflater().inflate(R.layout.menu_top_row, null);
    MenuItem menuItem=menu.add("");
    menuItem.setActionView(child);

解决方案

Taken from this stack overflow answer:

A PopupMenu is meant for displaying Menus and there really isn't a good way of customizing the appearance of the menu items. If you want something more flexible, your answer is ListPopupWindow.

private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    map.put(ICON, iconResourceId);
    data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
    ListPopupWindow popupWindow = new ListPopupWindow(this);

    ListAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[] {TITLE, ICON}, // These are just the keys that the data uses
            new int[] {android.R.id.text1, android.R.id.icon}); // The view ids to map the data to


    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
    popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
    popupWindow.show();
}

这篇关于在溢出菜单自定义菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆