7

如何完全动态隐藏状态和导航

该应用程序包含一个带有应用栏/工具栏和 FAB 按钮的常规导航抽屉。

当切换到全屏时,导航和状态栏的内容会被滚开。屏幕上留下两个空条。我希望那些空栏隐藏起来。

我创建了一个最小的演示应用程序。左侧是常规应用程序。在推动晶圆厂时,应用程序应全屏显示。

在此处输入图像描述

我怎样才能让酒吧隐藏?

问题:请写下最小演示项目中需要哪些更改?

更新了第二种解决方案

@Roaim 提供的 GREAT 解决方案有效。基本是将android:fitsSystemWindows布局属性设置为 false。

如果您仍然无法显示和隐藏状态/导航栏,此解决方案可能会帮助您。

完全隐藏栏:

public static void hideSystemUI() {
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}

并显示所有条:

public static void showSystemUI() {
    if (getSupportActionBar() != null) {
        getSupportActionBar().show();
    }
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
4

3 回答 3

7

更新

问题在于您的布局文件。我只是着手android:fitsSystemWindows=false解决这个问题。我向您的仓库提出了拉取请求,我认为这可以解决您的问题。

在此处输入图像描述


您应该遵循以下官方文档:


在 Android 4.0 及更低版本上隐藏状态栏

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
    }
    ...
}

在 Android 4.1 及更高版本上隐藏状态栏

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

隐藏导航栏

    View decorView = getWindow().getDecorView();
    // Hide both the navigation bar and the status bar.
    // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
    // a general rule, you should design your app to hide the status bar whenever you
    // hide the navigation bar.
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
于 2020-05-12T17:30:24.863 回答
0

进入价值观>风格

现在在应用程序主题使用

使导航栏转换

<item name="android:windowTranslucentNavigation">true</item>

要删除它,使其颜色与您的布局相匹配

<item name="android:navigationBarColor">@color/white</item>

或者试试这个

public void FullScreencall() {
    if(Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
        View v = this.getWindow().getDecorView();
        v.setSystemUiVisibility(View.GONE);
    } else if(Build.VERSION.SDK_INT >= 19) {
        //for new api versions.
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
    }
}
于 2020-05-03T11:30:42.783 回答
0

尝试这个...

<style name="MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
 ...
 <item name="windowNoTitle">true</item>
 <item name="windowActionBar">false</item>
 <item name="android:windowFullscreen">true</item>
 ...
 </style>
于 2021-12-23T10:00:22.307 回答