作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有谁知道 Android 版 Google map 官方应用程序的新版本 6 中的菜单使用的是什么组件?
我正在尝试构建一个类似的菜单,在官方开发页面中找不到任何内容(注意:我的目标是 Gingerbread API,可能向后兼容到 1.6。)
这是我找到的关于此菜单的唯一图片(这是在 ICS 上,但在 Gingerbread 上显示类似的内容)。请在此处查看左侧屏幕截图(来自 Gizmodo 网站):
如果没有内置组件,您会采用什么方法来构建一个?
最坏的情况是,如果 Android 2.x 没有这样的组件,您是否知道 Google Map 应用程序本身是否开源,以及在哪里可以找到它的源代码?
最佳答案
这应该适用于 API 4(但未经测试,YMMV)。例如:
如果您使用的是 ActionBarSherlock,则可以使用 IcsListPopupWindow
类。在 onCreate 中为其设置一些属性。您还需要继承 ArrayAdapter。
在 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
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;
}
}
每当您需要显示它时(例如在 View.OnClickListener
中)
mPopupMenu.setContentWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth() / 2);
PopupAdapter.notifyDataSetChanged(); // if you change anything
mPopupMenu.setAnchorView(yourAnchorView);
mPopupMenu.show();
在您的 OnItemClickListener 中
确保调用 mPopupMenu.dismiss()
!
希望对您有所帮助!感谢 Jake Wharton 的 ABS!
关于带图标的 Android 弹出菜单(类似于 Google Map 应用程序版本 6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325582/
我是一名优秀的程序员,十分优秀!