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

时光总嘲笑我的痴心妄想 提交于 2019-11-26 20:29:50

问题


Does anyone know what component is used for the menu in the new version 6 of Google Map’s official app on Android?

I’m trying to build a menu similar to that, couldn’t find anything in the official dev pages (Note: I’m targeting Gingerbread APIs, possibly with backward compatibility up to 1.6.)

Here is the only picture I found of this menu (this is on ICS, but something similar is displayed on Gingerbread). Please have a look at the left screenshot here (from the Gizmodo site):

If there’s no built-in component, what approach would you follow to build one?

At worst, if no such component exist for Android 2.x, do you know whether the Google Map application itself is open-source, and where to find its source?


回答1:


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/




回答2:


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!




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/8325582/android-pop-up-menu-with-icons-similar-to-google-map-app-version-6

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