从Android的菜单选项不工作弹出窗口 [英] popup window from android options menu not working
本文介绍了从Android的菜单选项不工作弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其实我显示弹出式窗口(自定义布局)时,选项菜单项,并单击我的选项菜单是底部到屏幕(splitActionBarWhenNarrow)。我是得到一些异常,请帮助我
code:
开关(item.getItemId()){
案例R.id.redid: Toast.makeText(MainActivity.this,红色,Toast.LENGTH_SHORT).show();
打破;
案例R.id.blueid:
Toast.makeText(MainActivity.this,蓝色,Toast.LENGTH_SHORT).show();
打破;
案例R.id.greenid: LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看popupview = inflater.inflate(R.layout.popuplayout,NULL);
PopupWindow popwindow =新PopupWindow(popupview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popwindow.showAsDropDown(item.getActionView(),100,100);
解决方案
尝试以这种方式:
开关(item.getItemId()){
案例R.id.redid: Toast.makeText(MainActivity.this,红色,Toast.LENGTH_SHORT).show();
打破;
案例R.id.blueid:
Toast.makeText(MainActivity.this,蓝色,Toast.LENGTH_SHORT).show();
打破;
案例R.id.greenid:
initiatePopupWindow();
打破;
}
和外面的onCreate()粘贴:
私人PopupWindow pwindo;私人无效initiatePopupWindow(){
尝试{
//我们需要得到LayoutInflater的实例
LayoutInflater吹气=(LayoutInflater)PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.popup,(ViewGroup中)findViewById(R.id.popup_element));
pwindo =新PopupWindow(布局,350,350,TRUE);
pwindo.showAtLocation(布局,Gravity.CENTER,0,0);btnClosePopup =(按钮)layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);}赶上(例外五){
e.printStackTrace();
}
}
我觉得这种方式可能是你的问题会得到解决。
Actually I am showing popup window(custom layout) when options menu item clicked and my options menu are bottom to the screen(splitActionBarWhenNarrow).I am getting some exception please help me code:
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);
解决方案
Try in this manner:
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;
}
and outside onCreate() paste this:
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();
}
}
I think in this manner may be your problem will be solved.
这篇关于从Android的菜单选项不工作弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文