2

在 Android API 23 中,我有一个带有子菜单项的弹出菜单。我已经定义了重力到最后,如图

popupMenu = new PopupMenu(this, v, Gravity.END);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_reminder:
                // Option1
                // ... other options / not relevant for the question    
popupMenu.inflate(R.menu.menu_contact_context);
popupMenu.show();

菜单有几个子菜单,在 xml 中定义

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="es.goodsal.mobile.ContactActivity">
<item
    android:id="@+id/add"
    android:icon="@android:drawable/ic_menu_add"
    android:title="@string/action_add"
    >
    <menu>
        <item
            android:id="@+id/action_add_reminder"
            android:icon="@drawable/ic_action_add_reminder"
            android:orderInCategory="100"
            android:title="@string/action_add_reminder"
            />
        <item
            android:id="@+id/action_add_note"
            android:icon="@drawable/ic_action_add_note"
            android:orderInCategory="100"
            android:title="@string/action_add_note"/>
        <item
            android:id="@+id/action_add_opportunity"
            android:icon="@drawable/ic_action_add_opportunity"
            android:orderInCategory="100"
            android:title="@string/action_add_opportunity"/>
        <item
            android:id="@+id/action_add_agenda"
            android:icon="@drawable/ic_action_add_agenda"
            android:orderInCategory="100"
            android:title="@string/action_add_agenda"/>
        <item
            android:id="@+id/action_add_task"
            android:icon="@drawable/ic_action_add_task"
            android:orderInCategory="100"
            android:title="@string/action_add_task"/>
    </menu>
</item>
<item
    android:id="@+id/action_contact_edit"
    android:icon="@android:drawable/ic_menu_edit"
    android:title="@string/action_edit"/>
<item
    android:id="@+id/action_contact_record"
    android:icon="@drawable/ic_contact_default"
    android:title="@string/action_contact"/>
<item
    android:id="@+id/action_report"
    android:title="@string/action_report"/>
<item
    android:id="@+id/funnel"
    android:icon="@drawable/ic_action_funnel"
    android:title="@string/action_funnel"
    >
    <menu>
        <item
            android:id="@+id/action_funnel_progress"
            android:orderInCategory="100"
            android:title="@string/action_funnel_progress"
            />
        <item
            android:id="@+id/action_funnel_regress"
            android:orderInCategory="100"
            android:title="@string/action_funnel_regress"
            />
    </menu>
</item>
</menu>

我的问题是菜单何时显示。原始菜单向右对齐(=Gravity.END),但子菜单向左对齐,我找不到为子菜单定义重力的方法(而且,为什么不一样?!?)

4

1 回答 1

1

不是一个真正的答案,但我刚刚发布了一个库,用于构建更好的嵌套菜单,因为我对 PopupMenu 感到沮丧:https ://github.com/saket/cascade

它旨在成为以下设备的替代品PopupMenu

- val popup = PopupMenu(context, anchor)
+ val popup = CascadePopupMenu(context, anchor)
  popup.inflate(R.menu.nicolas_cage_movies)
  popup.show()
于 2020-10-16T23:06:19.210 回答