我正在尝试实现 flipkart 应用程序的操作栏功能。 .
为此,我已经成功地创建了一个自定义操作栏,但是我在将菜单显示为溢出图标点击时的下拉菜单时遇到了问题。
如果我尝试 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/