使用全屏隐藏android 4.4或kitkat中的状态栏

我正在使用以下代码隐藏状态栏以满足全屏目的:

    void HideEverything(){
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            View decorView = getWindow().getDecorView();
            // Hide the status bar.
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
            // Remember that you should never show the action bar if the
            // status bar is hidden, so hide that too if necessary.
            ActionBar actionBar = getActionBar();
            actionBar.hide();
        }
    }

它以全屏模式显示屏幕,但当我触摸状态栏时,它显示状态栏.
我尝试了其他选项在清单中的应用程序和活动级别添加全屏和没有标题栏主题,但仍然结果是相同的…… 🙁

我不想在任何步骤显示状态栏.
如何在android中实现这一点?

编辑:

Android OS版本是4.4.2
(以上代码适用于4.3但不适用于4.4.2)

更新:

问题已经解决了……
答案分别写在下面……

没有解决方案可以解决这个问题,因为Android版本是4.4.2.

由@Academy of Programmer评论;此解决方案不适用于Android 6及更高版本.

但是根据Sunny的建议,通过防止状态栏的扩展解决了问题.

解决方案是:

在主活动中编写内联customViewGroup类.

public class customViewGroup extends ViewGroup {

        public customViewGroup(Context context) {
            super(context);
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            Log.v("customViewGroup", "**********Intercepted");
            return true;
        }
    }

现在在mainActivity的onCreate方法中,在setContentView()之前添加以下代码:

WindowManager manager = ((WindowManager) getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE));

        WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
        localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        localLayoutParams.gravity = Gravity.TOP;
        localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

                // this is to enable the notification to receive touch events
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

                // Draws over status bar
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        localLayoutParams.height = (int) (50 * getResources()
                .getDisplayMetrics().scaledDensity);
        localLayoutParams.format = PixelFormat.TRANSPARENT;

        customViewGroup view = new customViewGroup(this);

        manager.addView(view, localLayoutParams);

上面的代码将禁用状态栏的扩展.
现在通过以下代码全屏显示,在上面的代码下面和setContentView之前添加它:

//fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

现在在清单中添加权限:

<!-- prevent expanding status bar -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

你完成了…. 🙂

缺点:此代码禁用设备状态栏的扩展,而不仅仅是活动或应用程序,因此如果您确实需要它,请使用它.

感谢您提供的所有解决方案和建议…… 🙂

https://stackoverflow.com/questions/32984476/hide-status-bar-in-android-4-4-or-kitkat-with-fullscreen

转载注明原文:使用全屏隐藏android 4.4或kitkat中的状态栏