How to create dialog that looks exactly like Conte

2019-04-11 05:29发布

问题:

Sometimes it`s not convenient to use standart activity methods for creating ContextMenu (because there is can be many places in one activity but in different classes where it would be necessary to show contextmenu).

But creating dialogs is easy from any place. How can I create dialog that looks exactly like ContextMenu? Where can I find layout of standart context menu or something like that?

回答1:

I decide to use AlertDialogs with list (http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList) instead of ContextMenu. It looks like ContextMenu (perhaps, not exactly, because of different layouts), but it's more flexible, cause I can handle different actions in different listeners.



回答2:

You can find MenuBuilder, ContextMenuBuilder and MenuDialogHelper useful (see code here). In particular, MenuDialogHelper is the closest thing to what you are trying to achieve.

Pay attention to next menu type:

/**
 * The menu type that represents a menu dialog. Examples are context and sub
 * menus. This menu type will not have a corresponding MenuView, but it will
 * have an ItemView.
 */
public static final int TYPE_DIALOG = 2;

Combined with this lines:

// Order must be the same order as the TYPE_*
static final int ITEM_LAYOUT_RES_FOR_TYPE[] = new int[] {
    com.android.internal.R.layout.icon_menu_item_layout,
    com.android.internal.R.layout.list_menu_item_layout,
    com.android.internal.R.layout.list_menu_item_layout,
};

Its clear that list_menu_item_layout is the layout you are looking for. It can be found here. This layout represent single item in context menu list view.