点击菜单项时,我会打开PopupWindow
。现在我在不同的设备上测试了我的应用并认识到它在Android 2.3.3,4.0.3,4.0.4中运行不正常(在4.2.2中它工作正常)
LayoutInflater inflaterSettings = LayoutInflater.from(context);
final LinearLayout llSettings = (LinearLayout) inflaterSettings.inflate(R.layout.actionbar_menu_settings_popup, null);
...
if (popUp != null && popUp.isShowing())
popUp.dismiss();
else if (popUp == null)
popUp = new PopupWindow(context);
popUp.setContentView(ll);
ll.post(new Runnable()
{
@Override
public void run()
{
Log.i("MainActivity#onOptionsItemSelected#run", "start run");
popUp.showAtLocation(ll, Gravity.CENTER, 0, 0);
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
width = (int) (width * 0.9);
height = (int) (height * 0.9);
popUp.update(width, height);
Log.i("MainActivity#onOptionsItemSelected#run", "end run");
}
});
Log.i("MainActivity#onOptionsItemSelected", "popup opened");
我想知道,因为它不会引发任何错误。如果我点击设备上的菜单按钮,然后点击应打开弹出窗口的项目,菜单(显示项目)将被关闭但弹出窗口不会显示。如果我之后第二次点击菜单按钮,将执行run方法并打开弹出窗口。
是什么原因?如何在早期的Android版本中操作它可以正常运行的代码?