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

Jyo*_*iff 2 android fullscreen statusbar hide 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:

Android操作系统版本为4.4.2(以上代码适用于4.3但不适用于4.4.2)

更新:

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

Jyo*_*iff 6

没有解决方案可以解决这个问题,因为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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在在您添加以下代码的onCreate方法mainActivity之前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);
Run Code Online (Sandbox Code Playgroud)

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

//fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);   
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Run Code Online (Sandbox Code Playgroud)

现在在manifest中添加权限:

<!-- prevent expanding status bar -->
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)

你完成了.... :)

缺点:此代码disables expansion of status bar for the device不仅适用于活动或应用程序,因此如果您确实需要它,请使用它.

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