简体   繁体   English

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

[英]Hide status bar in android 4.4+ or kitkat with Fullscreen

I'm using following code for hiding status bar with full screen purpose: 我正在使用以下代码隐藏状态栏以实现全屏:

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

It shows Screen in full-screen mode but when I touch status bar it shows status bar. 它以全屏模式显示屏幕,但当我触摸状态栏时,它显示状态栏。 I tried other options to add the full screen and no title bar theme in application and activity level in manifest, but still results are the same... :( 我尝试了其他选项在清单中的应用程序和活动级别添加全屏和没有标题栏主题,但仍然结果是相同的... :(

I don't want to show status bar at any step. 我不想在任何步骤显示状态栏。 How to achieve this in android? 如何在android中实现这一点?

Edit : 编辑:

Android OS version is 4.4.2 (Above code is working on 4.3 but not on 4.4.2) Android操作系统版本为4.4.2(以上代码适用于4.3但不适用于4.4.2)

Update: 更新:

Problem has been solved... Answer is written below separately... 问题已经解决了......答案分别写在下面......

No solution could work for the problem as Android version is 4.4.2. 没有解决方案可以解决这个问题,因为Android版本是4.4.2。

As commented by @Academy of Programmer; 由@Academy of Programmer评论; this solution does not work on Android 6 and above. 此解决方案不适用于Android 6及更高版本。

But as per the suggestion by Sunny , problem has been solved by preventing the expansion of status bar. 但是根据Sunny的建议,通过阻止状态栏的扩展解决了问题。

Solution is: 解决方案是:

Write an inline customViewGroup class in your main activity. 在主活动中编写内联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;
        }
    }

Now in your onCreate method of mainActivity add following code before setContentView() : 现在在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);

Above code will disable the expansion of status bar. 上面的代码将禁用状态栏的扩展。 Now make full screen by following code, add it just below the above code and before the setContentView : 现在通过以下代码全屏显示,在上面的代码下面和setContentView之前添加它:

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

Now in manifest add the permission: 现在在manifest中添加权限:

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

and you are done.... :) 你完成了.... :)

drawback : This code disables expansion of status bar for the device not just for an activity or an application, so use it if you really need it. 缺点:此代码disables expansion of status bar for the device而不仅仅是活动或应用程序,因此如果您确实需要它,请使用它。

Thank you for all the solutions and suggestions... :) 感谢您提供的所有解决方案和建议...... :)

We cannot prevent the status appearing in full screen mode in kitkat or above devices, so try a hack to block the status bar from expanding. 我们无法阻止在kitkat或更高版本设备中以全屏模式显示状态,因此请尝试使用hack来阻止状态栏扩展。

For an idea, put an overlay over status bar and consume all input events. 对于一个想法,将覆盖放在状态栏上并使用所有输入事件。 It will prevent the status bar from expanding. 它将阻止状态栏扩展。

Using below code you can use full screen ; 使用以下代码,您可以全屏使用;

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

Write below code above setcontentview. 在setcontentview上面写下面的代码。

Note : Don't give any theme to this activity. 注意:不要为此活动提供任何主题。

Hope this helps. 希望这可以帮助。

use following code onCreate 使用以下代码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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM