Android

时间:2016-06-12 14:26:22

标签: android service android-service layoutparams android-windowmanager

我已经制作了一项服务,在每个活动的顶部放置黑色矩形,除了状态/通知栏和导航栏外,屏幕变黑。我的目标是隐藏一切。如何让这个黑色矩形全屏?我尝试过使用FLAG_FULLSCREENFLAG_LAYOUT_IN_SCREEN,但它没有用。我已经阅读过关于禁用TypeGuard的内容,但即使这样也没有用。提前谢谢!

这是我的代码

    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    ImageView black = new ImageView(this);
    black.setImageResource(R.drawable.black);

    LinearLayout layout = new LinearLayout(this);
    layout.addView(black);

    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_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 0;

    windowManager.addView(layout, params);

3 个答案:

答案 0 :(得分:1)

我解决了这个程序

    if (null == mInflater) {
        mInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }if (mLockscreenView == null) mLockscreenView = mInflater.inflate(R.layout.view_locokscreen, null);

mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
            mWindowManager.addView(mLockscreenView, mParams);
            final Handler handler = new Handler();
            mLockscreenView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mLockscreenView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                        }
                    },2000);
                }
            });

答案 1 :(得分:0)

i solved this issue just simply adding one line

layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

答案 2 :(得分:-1)

试试这个:

assert getSupportActionBar() != null;
getSupportActionBar().hide();   
mContentView = findViewById(R.id.fullscreen_content);
mContentView.setSystemUiVisibility(View.GONE | View.SYSTEM_UI_FLAG_FULLSCREEN);

fullscreen_content是父布局。