我创建了一个PopupMenu,当我点击一个按钮时它会显示,但它会显示一个菜单项列表。我想知道是否有办法让它成为网格而不是列表。
button = (Button) findViewById(R.id.menu_btn);
final PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
popupMenu.getMenu().add("item 1");
popupMenu.getMenu().add("item 2");
popupMenu.getMenu().add("item 3");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupMenu.show();
}
});
答案 0 :(得分:0)
您可以创建一个自定义PopupWindow
布局,使其完全符合您的要求,并在show()
功能之前对其进行充气。在http://developer.android.com/guide/topics/ui/menus.html#PopupMenu或此处How to create a custom PopupMenu in Android,您可以找到一些示例。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用自定义对话框来实现您的目标,请查看Custom dialog
这会有所帮助。
答案 3 :(得分:0)
我实际上想出了一些方法来做到这一点
static final String TITLE = "title";
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
PopupWindow popupMessage;
ImageButton popupButton;
GridView gridview;
View view;
// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TITLE, title);
data.add(map);
}
public void init() {
popupButton = (ImageButton) findViewById(R.id.menu_btn_btn);
view = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
gridview = (GridView) view.findViewById(R.id.popup_window_gridview);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("clicked", "ok");
}
});
SimpleAdapter adapter = new SimpleAdapter(
this,
data,
android.R.layout.activity_list_item, // You may want to use your own cool layout
new String[]{TITLE}, // These are just the keys that the data uses
new int[]{android.R.id.text1}); // The view ids to map the data to
addItem("one");
addItem("two");
addItem("three");
addItem("four");
addItem("five");
addItem("six";
gridview.setAdapter(adapter);
}
public void popupInit() {
popupButton.setOnClickListener(this);
popupMessage = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
popupMessage.setContentView(view);
}
onCreate()
之后init()和 popupInit()
不幸的是,gridView onItemClickListner实际上并没有工作,我仍然没有想出原因!