【问题标题】:popup window from android options menu not workingandroid选项菜单中的弹出窗口不起作用
【发布时间】:2014-01-07 18:49:36
【问题描述】:

实际上,当单击选项菜单项并且我的选项菜单位于屏幕底部时,我正在显示弹出窗口(自定义布局)(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);  

【问题讨论】:

  • 那么您想在 greenid 下显示 PopupWindow 吗?你能看到 redid 和 blueid 的 Toast 消息吗?
  • 是的,吐司也很好用

标签: android popupwindow android-optionsmenu


【解决方案1】:

以这种方式尝试:

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

我认为这种方式可能会解决你的问题。

【讨论】:

  • @Peter 如果我的回答有用,请投票,这会提供额外的能量。 :) 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 2018-12-17
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多