The best way to create drop down menu in android 2.x like in ICS

删除回忆录丶 提交于 2019-11-28 07:37:31

If your purpose is to recreate an action bar, you could take a look at the ActionBarSherlock project, which backports thoses functionalities.

If you want to be able to pop such a menu anywhere in your app, you could read the implementation of the MenuPopupHelper class implementation in the ActionBarSherlock project source code.

I use PopupWindow for that sort of thing. It's more work than a PopupMenu, where you just give it a Menu and respond to events with OnMenuItemClickListener.onMenuItemClick(). With a PopupWindow, you provide a content View and handle clicks at a somewhat lower level.

Still, it gets the job done without too much trouble. The showAsDropDown() method allows you to anchor the popup to another View (position it appropriately as a drop-down or pop-up menu), just as you want. It's been around since API level 1 and works great for me on all my test devices.

An alternative to your requirement could be ,that you create a list view,, position it under your button & set it's visibility to invisible by default & when you click on the button you can toggle the listview's visibility .... for example

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (LISTVIEW.isShown()) {
                    LISTVIEW.setVisibility(View.INVISIBLE);
                } else {
                    LISTVIEW.setVisibility(View.VISIBLE);
                }

let me know if this helps...

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