如何动态隐藏状态和导航栏完全?
[该应用包含一个常规的导航抽屉,其中包含一个应用栏/工具栏和FAB按钮。
切换到全屏时,导航和状态栏的内容会滚动显示。屏幕上留下两个空白栏。我希望隐藏那些空条。
我创建了一个minimal demo app。左侧是常规应用。在工厂上推时,该应用应全屏显示。
如何隐藏条形图?
问题:请写出最小演示项目中需要进行哪些更改?
我尝试了许多不同的选择。
替代1:只是将其隐藏
View decorView = MainActivity.mainActivity.getWindow().getDecorView();
decorView.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);
不,不起作用。更改colorPrimaryDark(下图)时,该颜色将显示在栏上。
<style name="Base.Theme.Xyz" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorPrimaryDark">@color/my_primary_dark</item>
Alternative-2:我使用IMMERSE_STICKY,但这与行为无关紧要。
Alternative-3:之前,我还尝试了这些样式项的组合:没有任何变化。
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Alternative-4:我读了this famous post on full screen,但没有帮助。
Alternative-5:另一个著名的技巧没有帮助:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
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
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Alternative-6:回到可行的方式。得到更好的诊断。
[阅读this post之后,我首先创建了一个简单的全屏应用程序(AS> new ...)。很好然后,我开始使用导航抽屉创建标准应用程序。然后,我开始将全屏应用程序的基本代码移至“导航抽屉”应用程序...,这给出了相同的行为。结果在项目中。
输入值>样式
现在在应用程序中使用主题
使导航栏翻译
<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 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);