Android pop-up menu with icons (similar to Google Map app version 6)

随声附和 提交于 2019-11-27 20:56:49

Just look into following link. There is an good examples of Quick Action dialog.So you can modify the code for whatever you want.

http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/

This should work down to API 4 (but not tested, YMMV). For example:

If you are using ActionBarSherlock, you can use the IcsListPopupWindow class. Set up some properties on it in onCreate. You'll need to subclass an ArrayAdapter as well.

in onCreate():

mPopupMenu = new IcsListPopupWindow(getContext());
mAdapter = new PopupMenuAdapter(this, android.R.layout.simple_list_item_1, yourArrayOfPopupMenuItems);
mPopupMenu.setAdapter(mAdapter);
mPopupMenu.setModal(true);
mPopupMenu.setOnItemClickListener(this);
mPopupMenu.setOnDismissListener(this); // only if you need it

Inner classes within your fragment/activity:

private class PopupMenuAdapter extends ArrayAdapter<PopupMenuItem> {

    Context context;
    int layoutResourceId;
    PopupMenuItem data[] = null;

    public PopupMenuAdapter(Context context, int layoutResourceId, PopupMenuItem[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        // initialize a view first
        if (view == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            view = inflater.inflate(layoutResourceId, parent, false);
        }

        PopupMenuItem pItem = data[position];
        TextView text = (TextView)view.findViewById(android.R.id.text1);
        text.setText(pItem.textResId);
        text.setCompoundDrawablesWithIntrinsicBounds(pItem.iconResId, 0, 0, 0);

        return view;
    }
}

// ... PopupMenuItem is just a container

private static class PopupMenuItem {
    public int iconResId;
    public int textResId;

    public PopupMenuItem(int iconResId, int textResId) {
        this.iconResId = iconResId;
        this.textResId = textResId;
    }
}

Whenever you need to show it (such as in a a View.OnClickListener)

mPopupMenu.setContentWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth() / 2);
PopupAdapter.notifyDataSetChanged(); // if you change anything
mPopupMenu.setAnchorView(yourAnchorView);
mPopupMenu.show();

In your OnItemClickListener

Make sure to call mPopupMenu.dismiss()!

Hope this helps! And thanks to Jake Wharton for ABS!

PopupMenu is probably what you are looking for. However, it only works on Android 3.0+ (introduced in API Level 11) and it's not present in the compatibility library as far as I know.

This looks more like a customized action bar. Probably an ActionProvider. The ActionBar is available since API level 11, but check out ActionBarSherlock.

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