简体   繁体   English

如何在一项活动中隐藏 android 中的状态栏

[英]How to hide status bar in android in just one activity

I want to hide status bar / notification bar in splash screen I am using style: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> this dose not hide the status bar, how should i do this?我想在启动画面中隐藏状态栏/通知栏 我正在使用样式: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">这不会隐藏状态栏,我应该怎么做?

If you want to remove status bar then use this before setContentView(layout) in onCreateView method如果要删除状态栏,请在 onCreateView 方法中的 setContentView(layout) 之前使用它

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

credits学分

You can do something like below for appropriately setting theme on your particular activity -您可以执行以下操作,以便为您的特定活动适当设置主题 -

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/launch_theme</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
 </style>

Try this:尝试这个:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowFullscreen">true</item>
</style>

Use Below Code to solve your problem.使用下面的代码来解决您的问题。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Add Below line to hide 
        getSupportActionBar().hide();
    }

Try this complete solution for backward below android version 16 and forward compatible solution试试这个完整的解决方案,用于向后低于 android 版本 16 和向前兼容的解决方案

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

To make your activity fullscreen, add this code in onCreate before setContentView :要使您的活动全屏显示,请在setContentView之前在onCreate添加以下代码:

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

For Kotlin users.对于 Kotlin 用户。

In styles.xml add new style:styles.xml添加新样式:

<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Now in AndroidManifest.xml add newly created style:现在在AndroidManifest.xml添加新创建的样式:

 <activity
            android:name=".MainActivity"
            android:theme="@style/NoActionBarTheme"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"

 >...</activity>

Finally in MainActivity.kt make following changes:最后在MainActivity.kt进行以下更改:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Add below line to hide status bar
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}

Now the status bar should be gone:现在状态栏应该消失了:

在此处输入图片说明

if you are using this in theme app如果您在主题应用程序中使用它

Theme.MaterialComponents.NoActionBar Theme.MaterialComponents.NoActionBar

change to改成

Theme.AppCompat.NoActionBar Theme.AppCompat.NoActionBar

and add code in activity:并在活动中添加代码:

fun fullScreen(activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    if (activity.window.insetsController != null) {
        val insetsController = activity.window.insetsController
        if (insetsController != null) {
            insetsController.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
            insetsController.systemBarsBehavior =
                WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
} else {
    activity.window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
}

} }

Try this:尝试这个:

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

I did some magic code.我做了一些神奇的代码。 Buttons in the status bar can be clicked, but it's difficult.状态栏中的按钮可以点击,但是很难。 Part for 4.4 does not work well. 4.4 的部分效果不佳。

    /** Скрывает статус-бар */
    private void hideStatusBar(View decorView)
    {
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        android.app.ActionBar actionBar = getActionBar();

        if (actionBar != null)
            actionBar.hide();
    }

    @Override protected void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);

            try
            {
                // for < 4.0
                if (Build.VERSION.SDK_INT < 16)
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
                // for > 4.0
                else
                {
                    final View decorView = getWindow().getDecorView();

                    hideStatusBar(decorView);

                    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
                    {
                        @Override public void onSystemUiVisibilityChange(int visibility)
                        {
                            if (visibility != View.SYSTEM_UI_FLAG_FULLSCREEN)
                                hideStatusBar(decorView);
                        }
                    });
                }
            }
            catch (Throwable t)
            {
                Util.log("Ошибка при попытке свернуть статус бар");
                t.printStackTrace();
            }

            setContentView(R.layout.activity_main);
}

This is what I used and it worked perfectly.这就是我使用的,并且效果很好。

Add this code to your activity onCreate before setContentView :setContentView之前将此代码添加到您的活动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);

    }

If you want to hide status bar line and action bar, you have to change the line in themes.xml如果要隐藏状态栏行和操作栏,则必须更改themes.xml 中的行

   <style name="Theme.MyApp" 
           parent="Theme.MaterialComponents.DayNight.DarkActionBar">

to

   <style name="Theme.MyApp" 
            parent="Theme.MaterialComponents.DayNight.NoActionBar">

and this to activity class in which you don't want status bar line..这对于您不想要状态栏行的活动类..

     requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);

Plz make sure that you have to add these lines above this in onCreate() method.请确保您必须在 onCreate() 方法中在此之上添加这些行。

        setContentView(R.layout.activity_main)

If you want to remove the status bar then如果要删除状态栏,则

For Java适用于 Java

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

For Kotlin对于 Kotlin

    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN

For flutter对于 flutter

    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);

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

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