Android设置页面Activity全屏(隐藏导航栏、状态栏) 您所在的位置:网站首页 设置全面屏模式 Android设置页面Activity全屏(隐藏导航栏、状态栏)

Android设置页面Activity全屏(隐藏导航栏、状态栏)

2023-11-28 14:25| 来源: 网络整理| 查看: 265

Android设置Activity全屏的三种方式:

1、manifest中设置:在application中设置,或者指定activity设置。

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

或者

android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"

2、style中设置:定义自己的主题。

false//无ActionBar true //无标题 true //全屏 @android:color/transparent @drawable/start3//背景图 true

3、代码中设置:在setContentView 之前调用

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

注意: 当有全面屏手机可以显示虚拟按键时,比如小米8手机,就会出现白色的虚拟按键区域,如图所示: 在这里插入图片描述 按照google的官方办法,设置如下几个Flag就可以隐藏导航栏:

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

google对这个方案做了说明:

With this approach, touching anywhere on the screen causes the navigation bar (and status bar) to reappear and remain visible. The user interaction causes the flags to be be cleared.(触摸屏幕任何位置,导航栏都会重新出现并保持可见。因为用户交互导致设置的flag被清除了)Once the flags have been cleared, your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.(如果想要导航栏再次隐藏,就要重新设置flag)Where you set the UI flags makes a difference. If you hide the system bars in your activity’s onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won’t get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().(不同地方设置UI Flag效果会有影响。在onResume或onWindowFouncChanged()函数里设置flag永久生效)The method setSystemUiVisibility() only has an effect if the view you call it from is visible.(只有在View是可见状态下,调用setSystemUiVisiblity才会生效)Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.(离开当前view,会导致利用setSystemUiVisiblity()函数设置的flag被清除)

官方对这几个flag的解释:

SYSTEM_UI_FLAG_HIDE_NAVIGATION

和FLAG_FULLSCREEN、FLAG_LAYOUT_IN_SCREEN一起使用会暂时隐藏导航栏。一旦用户与界面发生交互,导航栏又会出现。

SYSTEM_UI_FLAG_IMMERSIVE_STICKY

只有和SYSTEM_UI_FLAG_FULLSCREEN、SYSTEM_UI_FLAG_HIDE_NAVIGATION其中的一个或两个一起使用时才会有效果。

当使用了SYSTEM_UI_FLAG_IMMERSIVE_STICKY标签的时候,向内滑动的操作会让系统栏临时显示,并处于半透明的状态(沉浸式)。此时没有标签会被清除,系统UI可见性监听器也不会被触发,布局的大小不会被影响。如果用户没有进行操作,系统栏会在一段时间内自动隐藏。

当你使用SYSTEM_UI_FLAG_IMMERSIVE标签的时候,它是基于其他设置过的标签(SYSTEM_UI_FLAG_HIDE_NAVIGATION和SYSTEM_UI_FLAG_FULLSCREEN)来隐藏系统栏的。当用户向内滑动,系统栏重新显示并保持可见。但是布局大小会被虚拟按键的高度所影响。

如果你不想虚拟按键影响高度。可以用如下:

用其他的UI标签(如SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION和SYSTEM_UI_FLAG_LAYOUT_STABLE)来防止系统栏隐藏时内容区域大小发生变化是一种很不错的方法。你也需要确保Action Bar和其他系统UI控件同时进行隐藏。下面这段代码展示了如何在不改变内容区域大小的情况下,隐藏与显示状态栏和导航栏。

上面的设置后,底部虚拟导航栏的确隐藏掉了,But,一旦你点击屏幕,导航栏就会再次出现,并且会拦截点击事件。 想要完美隐藏底部虚拟导航栏,解决方法如下:(特别注意不能设置沉浸式状态栏否则不生效)

public static void hideNavKey(Context context) { if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT //for new api versions. View decorView = ((Activity) context).getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; decorView.setSystemUiVisibility(uiOptions); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有