在android中创建一个带有多个自定义选项的汉堡按钮。当我单击该按钮时,我希望它显示带有相应图标的选项(如下图所示)。

我使用了弹出菜单,但是如何在弹出菜单选项中设置图标?
请帮帮我??
发布于 2015-08-25 14:57:41
您可以尝试使用ListPopUpWindow并将其锚定到汉堡按钮(如果您没有使用actionbar)
http://developer.android.com/reference/android/widget/ListPopupWindow.html
String [] items = new String[]{"Item1" , "Item2", "Item3"};
View anchor= (Button)findViewById(R.id.BurgerButton);
anchor.setOnClickListener(new OnClickListener(){
showpopup();
});
public void showpopup()
{
ListPopupWindow popup = new ListPopupWindow(this);
//Replace this adapter with a custom adapter and layout for a custom menu item
popup.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,items));
popup.setAnchorView(anchor);
popup.setWidth(200);
popup.show();
}发布于 2015-08-25 15:07:29
试试这个: popup_menu
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/one"
android:title="One"
android:icon="@drawable:ic_launcher"/>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu> 在activity on按钮中,单击add this:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method 希望它能有所帮助:)
对于菜单图标,显示添加:
// Force icons to show
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popup);
argTypes = new Class[] { boolean.class };
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
// Possible exceptions are NoSuchMethodError and NoSuchFieldError
//
// In either case, an exception indicates something is wrong with the reflection code, or the
// structure of the PopupMenu class or its dependencies has changed.
//
// These exceptions should never happen since we're shipping the AppCompat library in our own apk,
// but in the case that they do, we simply can't force icons to display, so log the error and
// show the menu normally.
Log.w("error", "error forcing menu icons to show", e);
popup.show();
return;
}https://stackoverflow.com/questions/32196901
复制相似问题