2014-10-27 40 views
0

下面是我使用的顯示彈出菜單的代碼,以便它出現在我的列表視圖Popmenu沒有在列表視圖行的中心對齊

lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       final int pos, long id) { 
      PopupMenu popup = new PopupMenu(context, arg1); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
       popup = new PopupMenu(context, arg1,Gravity.CENTER); 
      } 
      //Inflating the Popup using xml file 
      popup.getMenuInflater().inflate(R.menu.listmenu, popup.getMenu()); 
      ///some code 
      return true; 
     } 
    }); 

排的中心,但問題是popmenu總是出現在左側。 當我給Gravity.RIGHT時,popmenu確實出現在行的最右側。 中心顯示出了什麼問題?

回答

0

你可以給錨

showPopup(viewOfWherePopupmenuShows,popmenuNameList.size()); 


PopupMenu popMenu = new PopupMenu(Activity.this,v) 
int size =popmenuNameList.size(); 
for(int i =0; i <size; i++){ 
popMenu.getMenu().add(popmenuNameList.get(i)).setIcon(R.drawable.logo); 

} 
popMenu.show(); 

...

protected void showPopupMenu(View v, int size){ 
    //create instance 
    PopupMenu popup = new PopupMenu(Activity.this,v); 
    //inflating the popup using xml 
    popup.getMenuInflater().inflate(R.menu.menu_popup_list),popup.getMenu()); 

for(int i=0;i<size;i++){ 
popmenu.getMenu().add(0,i+99,0,"text").setcheckable(true).setChecked(false); 
} 
}