如何使用系统覆盖窗口隐藏导航栏

时间:2015-10-28 09:09:49

标签: android

LockScreenReceiver.java

public class LockScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        //If the screen was just turned on or it just booted up, start your Lock Activity
        if(action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_BOOT_COMPLETED))
        {

            //Create a window manager params
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                            | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);
            //makeFullScreen((Activity) context);

            WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

            final View myView = inflater.inflate(R.layout.my_view, null);

            myView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.d("TAG", "touch me");
                    myView.setVisibility(View.GONE);
                    return true;
                }
            });

            // Add layout to window manager
            wm.addView(myView, params);
        }
    }

}

我有什么:  我有如下所示的输出,这是一个点击它的视图,它的可见性消失了

enter image description here

我想做什么: 我想在加载上面的视图时隐藏后面的栏,中心按钮。

如何实现这个目标?

4 个答案:

答案 0 :(得分:6)

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.format = PixelFormat.RGBA_8888;
    lp.flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
            | WindowManager.LayoutParams.FLAG_FULLSCREEN;
    lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
    lp.width = ViewGroup.LayoutParams.MATCH_PARENT;

    int flag =  View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

    mRootView.setSystemUiVisibility(flag);
    mWindowManager.addView(mRootView, getLayoutParams());

不要使用WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,此标志会使导航栏始终显示。我不知道为什么。

答案 1 :(得分:0)

您可以使用SYSTEM_UI_FLAG_HIDE_NAVIGATION标志隐藏Android 4.0及更高版本上的导航栏。此代码段隐藏了导航栏和状态栏:

View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
              | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

来源:here

答案 2 :(得分:0)

从Android 4.4 KitKat开始,您可以使用“沉浸式”全屏模式。这是详细信息和一些示例代码。

https://developer.android.com/training/system-ui/immersive.html

答案 3 :(得分:0)

尝试沉浸模式-SYSTEM_UI_FLAG_IMMERSIVE标志用于setSystemUiVisibility()