开发者

How to create dialog that looks exactly like ContextMenu?

开发者 https://www.devze.com 2023-03-26 08:40 出处:网络
Sometimes it`s not convenient to use standart activity methods for creating ContextMenu (bec开发者_如何学编程ause there is can be many places in one activity but in different classes where it would be

Sometimes it`s not convenient to use standart activity methods for creating ContextMenu (bec开发者_如何学编程ause 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消