android - 按钮上的自定义菜单单击作为下拉菜单

标签 android android-alertdialog android-dialog android-styles optionmenu

我正在尝试实现 flipkart 应用程序的操作栏功能。 Flipkart App screenshot .

为此,我已经成功地创建了一个自定义操作栏,但是我在将菜单显示为溢出图标点击时的下拉菜单时遇到了问题。

如果我尝试 Android Option Menu on button click然后我在屏幕的底部中心看到没有图标的菜单 (4.2.2)。使用自定义对话框或警报对话框会使背景变淡,并且它们还会出现在屏幕中央。

我应该怎么做才能获得与上图所示类似的功能?

最佳答案

您可以轻松地使用 ListPopupWindow 和任何方便您的 adapter:

// create any adapter with any data, just as for ordinary ListView
    SimpleAdapter simpleAdapter= new SimpleAdapter(this, dataSet, android.R.layout.simple_list_item_2,
                            new String[] {"Name", "Tel. no."},
                            new int[] {android.R.id.text1, android.R.id.text2});

ListPopupWindow listPopup = new ListPopupWindow(this); // feed context to ListPopupWindow
listPopup.setAdapter(simpleAdapter); // set adapter to created ListPopupMenu

listPopup.setAnchorView(findViewById(id)); 
// id - any id of any view on the screen. Under this view popup appears. It may be MenuItem as well

listPopup.setWidth(getWidestView(simpleAdapter)); 
// as popup width equals the anchor width, use getWidestView method for proper list displaying. 
// ListPopupWindow.WRAP_CONTENT won't work here

listPopup.show();
您可以在此处找到

getWidestView fragment :https://stackoverflow.com/a/13959716/4890659

关于android - 按钮上的自定义菜单单击作为下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883936/

相关文章:

java - 如何在 Android 中验证 Activity 后设置焦点 EditText?

android - 使用样式的警报对话框文本颜色

android - 对话框 : dim everything except some region

android - 为什么我得到 "Configuration ' compile' is obsolete”

android - 我应该在 Android-AppEngine 系统的 Android App 部分使用 Objectify Annotated Objects 吗?

android - 当 imageuri 作为额外传递时,Espresso 测试相机 Intent

java - 安卓 : application crashes with BadTokenException Error (trying to show AlertDialog)

android - 在 AlertDialog 中设置 EditText 的边距

java - 安卓 Java : How do I prevent my Dialog box from showing MainActivity appname briefly when the Dialog closes?

Android:在自定义对话框标题中使用默认样式