在 android 4.4+ 或全屏 kitkat 中隐藏状态栏

Hide status bar in android 4.4+ or kitkat with Fullscreen

我使用以下代码隐藏全屏状态栏:

    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();
        }
    }

它在 full-screen 模式下显示屏幕,但是当我触摸状态栏时它显示状态栏。 我尝试了其他选项以在应用程序中添加全屏和无标题栏主题,并在清单中添加 activity 级别,但结果仍然相同...:(

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

编辑:

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

更新:

问题已解决... 答案单独写在下面...

我们无法阻止状态栏在 kitkat 或更高版本的设备中以全屏模式出现,因此尝试破解以阻止状态栏展开。

对于一个想法,在状态栏上放置一个叠加层并消耗所有输入事件。它将阻止状态栏展开。

使用下面的代码onCreate

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);   
 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
 }

使用下面的代码你可以使用全屏;

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

在setcontentview上面写下面的代码。

Note : Don't give any theme to this activity.

希望对您有所帮助。

由于 Android 版本是 4.4.2,因此无法解决该问题。

据@程序员学院点评;此解决方案不适用于 Android 6 及更高版本。

但是根据Sunny的建议,问题已经通过阻止状态栏的扩展解决了。

解决方案是:

在你的主 activity.

中写一个内联 customViewGroup class
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;
        }
    }

现在在 mainActivityonCreate 方法中,在 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"/>

大功告成....:)

缺点: 这段代码 disables expansion of status bar for the device 不只是一个 activity 或一个应用程序,所以使用它如果你真的需要它。

感谢您提供的所有解决方案和建议...:)