Android - 为带有导航抽屉和应用栏的应用完全隐藏状态和导航栏
Android - hide status and navigation bar completely for an app with nav drawer and app bar
如何完全动态隐藏状态和导航栏?
该应用包含一个带有应用栏/工具栏和 FAB 按钮的常规导航抽屉。
切换到全屏时,导航和状态栏的内容被滚动掉了。屏幕上留下两个空栏。我想让那些空条隐藏起来。
我创建了一个 minimal demo app。左边是常规应用程序。在 fab 上推送时,应用程序应全屏显示。
如何隐藏栏?
问题:请写出最小演示项目中需要进行哪些更改?
已更新第二个解决方案:
@Roaim 提供的 GREAT 解决方案有效。重要的是将 android:fitsSystemWindows 布局 属性 设置为 false。
如果您仍然无法显示和隐藏 status/navigation 条,此解决方案可能会对您有所帮助。
完全隐藏条形图:
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);
}
进入价值观>风格
现在应用内主题使用
让导航栏transculate
<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);
}
}
更新
问题出在您的布局文件上。我只是设置 android:fitsSystemWindows=false
来解决这个问题。我对您的存储库进行了 pull request,我认为这可以解决您的问题。
您应该遵循以下官方文档:
Hide the Status Bar on Android 4.0 and Lower
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);
}
...
}
Hide the Status Bar on Android 4.1 and Higher
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();
Hide the Navigation Bar
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);
试试这个...
<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>
试试这个 hide/show 状态栏和导航栏,它们都是 SystemUI 的一部分。
fun setSystemUIVisibility(hide: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val window = window.insetsController!!
val windows = WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()
if (hide) window.hide(windows) else window.show(windows)
// needed for hide, doesn't do anything in show
window.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
val view = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = if (hide) view else view.inv()
}
}
如何完全动态隐藏状态和导航栏?
该应用包含一个带有应用栏/工具栏和 FAB 按钮的常规导航抽屉。
切换到全屏时,导航和状态栏的内容被滚动掉了。屏幕上留下两个空栏。我想让那些空条隐藏起来。
我创建了一个 minimal demo app。左边是常规应用程序。在 fab 上推送时,应用程序应全屏显示。
如何隐藏栏?
问题:请写出最小演示项目中需要进行哪些更改?
已更新第二个解决方案:
@Roaim 提供的 GREAT 解决方案有效。重要的是将 android:fitsSystemWindows 布局 属性 设置为 false。
如果您仍然无法显示和隐藏 status/navigation 条,此解决方案可能会对您有所帮助。
完全隐藏条形图:
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);
}
进入价值观>风格
现在应用内主题使用
让导航栏transculate
<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);
}
}
更新
问题出在您的布局文件上。我只是设置 android:fitsSystemWindows=false
来解决这个问题。我对您的存储库进行了 pull request,我认为这可以解决您的问题。
您应该遵循以下官方文档:
Hide the Status Bar on Android 4.0 and Lower
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);
}
...
}
Hide the Status Bar on Android 4.1 and Higher
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();
Hide the Navigation Bar
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);
试试这个...
<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>
试试这个 hide/show 状态栏和导航栏,它们都是 SystemUI 的一部分。
fun setSystemUIVisibility(hide: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val window = window.insetsController!!
val windows = WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()
if (hide) window.hide(windows) else window.show(windows)
// needed for hide, doesn't do anything in show
window.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
val view = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = if (hide) view else view.inv()
}
}