有时,使用标准活动方法创建ContextMenu并不方便(因为在一个活动中会有很多不同的类别需要显示ContextMenu)。
但是,从任何地方创建对话框都很容易。 我该如何创建外观与ContextMenu完全相同的对话框?在哪里可以找到标准ContextMenu的布局或类似内容?
但是,从任何地方创建对话框都很容易。 我该如何创建外观与ContextMenu完全相同的对话框?在哪里可以找到标准ContextMenu的布局或类似内容?
我决定使用带有列表的AlertDialog(http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList)而不是ContextMenu。它看起来像ContextMenu(也许不完全相同因为布局不同),但更加灵活,因为我可以在不同的监听器中处理不同的操作。
MenuBuilder
、ContextMenuBuilder
和MenuDialogHelper
很有用(请参见此处的代码)。特别是,MenuDialogHelper
是最接近您想要实现的功能的东西。/**
* 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;
结合这些代码行:
// 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,
};
list_menu_item_layout
是你正在寻找的布局。可以在这里找到它。这个布局表示上下文菜单列表视图中的单个项目。