为什么在操作栏中打开popupmenu

时间:2013-06-21 11:10:05

标签: android actionbarsherlock popupmenu

我正在制作一个时间表应用程序,但是我遇到了一个奇怪的问题,我创建了一个弹出菜单,在点击一个操作栏项目后会打开。

弹出窗口有效,但会在操作栏内打开 ,我希望它在下面的视图中打开。

enter image description here

我的代码..

    @Override
    public void onCreateOptionsMenu(
          Menu menu, MenuInflater inflater) {
       inflater.inflate(R.menu.lists_choice_mode_mulitplue, menu);
    }
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);

        }
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
          // Handle item selection
          switch (item.getItemId()) {
          case R.id.inverse:
              showPopupMenu(this.getView());
              return true;
          }
          return false;
    }
     private void showPopupMenu(View v){
         final Activity activity = getSupportActivity();
           PopupMenu popupMenu = new PopupMenu(activity, v);
              popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());

              popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

           @Override
           public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(activity,
              item.toString(),
              Toast.LENGTH_LONG).show();
            return true;
           }
          });

              popupMenu.show();
          }

我的.xml布局文件

popup.xml

 <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
          <group android:id="@+id/group_popupmenu">
              <item android:id="@+id/menu1"
                  android:title="Popup menu item 1"/>
              <item android:id="@+id/menu2"
                  android:title="Popup menu item 2"/>
              <item android:id="@+id/menu3"
                  android:title="Popup menu item 3"/>
          </group>
        </menu>

我的操作栏按钮.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/inverse"
        android:showAsAction="always|withText"
        android:title="Week"
        android:titleCondensed="Week" />
</menu>

2 个答案:

答案 0 :(得分:3)

是的,现在已经解决了!

对showPopupMenu(this.getView());

的错误

它应该是动作栏中图标的ID ..如下......

 @Override
         public boolean onOptionsItemSelected(MenuItem item) {
              // Handle item selection
              switch (item.getItemId()) {
              case R.id.inverse:

                  showPopupMenu(R.id.inverse);
                  return true;
              }
              return false;
        }

中更改showpopupmenu(查看v)
 private void showPopupMenu(int id){

             final Activity activity = getSupportActivity();
             View v = activity.findViewById(id);
               PopupMenu popupMenu = new PopupMenu(activity, v);
                  popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());

                  popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

               @Override
               public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(activity,
                  item.toString(),
                  Toast.LENGTH_LONG).show();
                return true;
               }
              });


                      popupMenu.show();


              }

现在有效!谢谢你的答案,它没有帮助我,但感谢你!

答案 1 :(得分:0)

您正在夸大菜单布局,我认为您想要夸大您的活动布局。

检查此前一个问题,该问题显示如何给布局充气:

How to inflate one view with a layout