2014-10-27 59 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); 
} 
}