我想为我的应用程序添加一个自定义视图.为此,我使用WindowsManager:
final WindowManager wm = getWindowManager();
final View view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.game_menu, null);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_FULLSCREEN;
lp.dimAmount = (float) 0.6;
lp.format = PixelFormat.TRANSPARENT;
lp.windowAnimations = android.R.style.Animation_Dialog;
view.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d("12", "12");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
wm.removeView(view); // This I need to hide my menu
}
return false;
}
});
wm.addView(view, lp); // I add menu like in Angry Birds and other games
但是我无法捕获用于隐藏此视图的设备按键事件.
为什么在WindowsManager添加的视图中未调用我的密钥侦听器?如何通过按下设备后退键隐藏我的视图?
解决方法:
您可以添加这些setFocusableInTouchMode(true);在你看来