按钮上的自定义菜单单击下拉菜单

时间:2014-09-17 06:42:35

标签: android android-alertdialog android-dialog android-styles optionmenu

我正在尝试实现flipkart应用的操作栏功能。Flipkart App screenshot

为此我已经成功创建了一个自定义操作栏,但是我在将菜单显示为下拉溢出图标点击时遇到问题。

如果我尝试Android Option Menu on button click,那么我会在屏幕底部中心找到没有图标(4.2.2)的菜单。使用自定义对话框或警告对话框会淡出背景,它们也会显示在屏幕的中央。

如何使功能与上图所示类似?

5 个答案:

答案 0 :(得分:3)

我有一个简单的方法是将溢出菜单放在你身边,并将所有这些菜单添加为子菜单,通过这种方式你可以实现这个视图。此溢出菜单也会一直显示在ActionBar中

<item
    android:icon="@drawable/ic_action_overflow"
    android:showAsAction="always">
    <menu>

        <item
            android:id="@+id/menu_login"
            android:icon="@drawable/login"
            android:title="login"/>
    </menu>
</item>

答案 1 :(得分:3)

您可以轻松使用ListPopupWindow以及任何方便的adapter

// create any adapter with any data, just as for ordinary ListView
    SimpleAdapter simpleAdapter= new SimpleAdapter(this, dataSet, android.R.layout.simple_list_item_2,
                            new String[] {"Name", "Tel. no."},
                            new int[] {android.R.id.text1, android.R.id.text2});

ListPopupWindow listPopup = new ListPopupWindow(this); // feed context to ListPopupWindow
listPopup.setAdapter(simpleAdapter); // set adapter to created ListPopupMenu

listPopup.setAnchorView(findViewById(id)); 
// id - any id of any view on the screen. Under this view popup appears. It may be MenuItem as well

listPopup.setWidth(getWidestView(simpleAdapter)); 
// as popup width equals the anchor width, use getWidestView method for proper list displaying. 
// ListPopupWindow.WRAP_CONTENT won't work here

listPopup.show();
您可以在此处找到

getWidestView摘要:https://stackoverflow.com/a/13959716/4890659

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

请按照链接说明显示带图像的弹出菜单项,您可以使用showAtLocation()将弹出窗口显示为所需位置。

检查: PopupMenu with icons

答案 3 :(得分:0)

如果您正在寻找更多材质弹出菜单并且不害怕Ko​​tlin,您可以尝试:https://github.com/zawadz88/MaterialPopupMenu - 无需使用XML声明菜单,您可以更轻松地处理点击。

答案 4 :(得分:-1)

The only way that I could get the icons to show was by calling setForceIcons before inflating the menu.

/**
 * Make a call to setForceIcons to cause icons to show.
 *
 * @param popup
 */
public static void showForceIcons (final PopupMenu popup)
{
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}