来自安卓选项菜单的弹出窗口不起作用



实际上,当选项菜单项单击并且我的选项菜单位于屏幕底部时,我正在显示弹出窗口(自定义布局)(splitActionBarWhenNarrow)。我得到一些例外,请帮助我法典:

    switch (item.getItemId()) {
    case R.id.redid:
        Toast.makeText(MainActivity.this,"red color", Toast.LENGTH_SHORT).show();
        break;
    case R.id.blueid:
        Toast.makeText(MainActivity.this,"blue color", Toast.LENGTH_SHORT).show();
        break;
   case R.id.greenid:
  LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View popupview=inflater.inflate(R.layout.popuplayout,null);
  PopupWindow popwindow=new PopupWindow(popupview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  popwindow.showAsDropDown(item.getActionView(), 100, 100);  

尝试以下方式:

    switch (item.getItemId()) {
    case R.id.redid:
        Toast.makeText(MainActivity.this,"red color", Toast.LENGTH_SHORT).show();
        break;
    case R.id.blueid:
        Toast.makeText(MainActivity.this,"blue color", Toast.LENGTH_SHORT).show();
        break;
   case R.id.greenid:
        initiatePopupWindow();
        break;
   }

并在 onCreate() 之外粘贴这个:

private PopupWindow pwindo;
private void initiatePopupWindow() { 
try { 
// We need to get the instance of the LayoutInflater 
LayoutInflater inflater = (LayoutInflater) PopupActivity.this 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element)); 
pwindo = new PopupWindow(layout, 350, 350, true); 
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup); 
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) { 
e.printStackTrace(); 
} 
}

我想以这种方式可能会解决你的问题。

最新更新