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

时间:2017-03-23 06:19:22

标签: android android-styles

我想隐藏我正在使用的启动画面中的状态栏/通知栏 风格:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 这个剂量不能隐藏状态栏,我该怎么做?

12 个答案:

答案 0 :(得分:39)

如果要删除状态栏,请在onCreateView方法中的setContentView(layout)之前使用它

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

credits

答案 1 :(得分:2)

使用以下代码解决您的问题。

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

答案 2 :(得分:1)

将该活动全屏显示。在onCreate

之前在该活动的setContentView中添加此代码
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

答案 3 :(得分:1)

尝试使用这个完整的解决方案,以便在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);
    }

答案 4 :(得分:1)

您可以执行以下操作来为您的特定活动适当设置主题-

<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>

答案 5 :(得分:1)

尝试一下:

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

答案 6 :(得分:1)

如果您在主题应用程序中使用它

Theme.MaterialComponents.NoActionBar

更改为

Theme.AppCompat.NoActionBar

并在活动中添加代码:

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

}

答案 7 :(得分:0)

试试这个:

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

答案 8 :(得分:0)

我做了一些魔术代码。可以单击状态栏中的按钮,但这很困难。用于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);
}

答案 9 :(得分:0)

这是我应该做的,并且代码运行良好。将此代码添加到您的活动中

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

答案 10 :(得分:0)

针对Kotlin用户。

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>

现在在AndroidManifest.xml中添加新创建的样式:

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

 >...</activity>

最后在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
}

现在状态栏应该消失了:

enter image description here

答案 11 :(得分:-2)

在Manifest文件上更新您的活动样式主题@style/Theme.AppCompat.Light.NoActionBar

<activity
    android:name=".Activity"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

</activity>