Popmenu未在listview行的中心对齐

时间:2014-10-27 11:54:32

标签: android listview popupmenu

以下是我用来显示popupmenu的代码,以便它出现在我的listview行的中心

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确实出现在行的最右侧。 中心显示器出了什么问题??

1 个答案:

答案 0 :(得分: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);
}
}