1
MainActivity extends android.support.v7.app.ActionBarActivity

MainActivity.java 中的代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    int id = item.getItemId();
    switch (id) {
    case R.id.action_sort:
        showSortPopup(item.getActionView());
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void showSortPopup(View view) {
    PopupMenu popup = new PopupMenu(MainActivity.this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.sort_menu, popup.getMenu());
    popup.show();
}

排序菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/action_ascending"
              android:title="Ascending" />
        <item android:id="@+id/action_descending"
              android:title="Descending" />
</menu>

这是选择R.id.action_sort操作栏菜单时显示的弹出菜单:

弹出错误预览

它有效,我的意思是我可以滚动它以查看其他菜单项,但为什么它没有展开?

4

0 回答 0