2013-06-21 49 views
2

我正在制定一個時間表應用程序,但我有一個奇怪的問題,我創建了一個popup菜單,點擊一個動作欄項目後打開。爲什麼在動作欄中打開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> 
+0

你能告訴我們你的xml佈局嗎? – Borys

回答

4

沒錯這就是現在的固定!

它錯了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(圖五)

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(); 


       } 

它現在!謝謝你的回答,但它並沒有幫助我,但我很感激!

相關問題