操作栏弹出菜单项不可见

时间:2016-12-20 09:51:59

标签: android android-popupwindow

我已经在按钮onClick上实现了弹出菜单,使用AppCompact主题我可以在菜单中获取菜单溢出的空白项目。请帮帮我

OptionMenuBtn = (ImageButton) v.findViewById(R.id.three_dot);
        OptionMenuBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated m// openOptionsMenu();
                showPopup(v);


            }
        });


    public void showPopup(View v) {

        PopupMenu popup = new PopupMenu(MainActivity.this, v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.action_menu, popup.getMenu());
        popup.show();
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

这应该是Xml文件:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/menu_edit"
    android:title="@string/menu_edit" />
<item
    android:id="@+id/menu_block"
    android:title="@string/menu_deactivate" /></menu>

以及onclick上的活动代码

  PopupMenu popupMenu = new PopupMenu(YourActivity.this, view);
            popupMenu.setOnMenuItemClickListener(YourActivity.this);
            popupMenu.inflate(R.menu.menu_import_export);
            popupMenu.show();

并实施:

  @Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
       case R.id.menu_edit:
           //TODO
            return true;
        case R.id.menu_block:
        //TODO 
            return true;

    }
    return false;
}