如何更改WindowManager.LayoutParams'以编程方式键入?

时间:2017-04-16 00:38:23

标签: android lockscreen android-windowmanager

我得到了一个可以在锁屏上显示的视图。但我想让它成为可选的。目前我使用这些参数来实现这一目标:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, 
WindowManager.LayoutParams.WRAP_CONTENT, 
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | 
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSPARENT);

为了不在锁屏上显示,我使用

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, 
WindowManager.LayoutParams.WRAP_CONTENT, 
WindowManager.LayoutParams.TYPE_PHONE, 
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | 
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSPARENT);

另外它们工作正常,但我想以编程方式更改类型和标志。所以我尝试用

更改WindowManager.LayoutParams
params.flags &= ~WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
params.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
params.flags |= WindowManager.LayoutParams.TYPE_PHONE;
windowManager.updateViewLayout(myView,params);

删除类型,删除标志并设置新类型, 但这似乎不起作用。有谁知道如何正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

您将type值设置为flag。试试这个:

params.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

// type is no bit flag, so this should do it
params.type = WindowManager.LayoutParams.TYPE_PHONE;