How to create dialog that looks exactly like ContextMenu?

 ̄綄美尐妖づ 提交于 2019-12-06 07:08:10

问题


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.



来源:https://stackoverflow.com/questions/6958622/how-to-create-dialog-that-looks-exactly-like-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!