Android设置页面Activity全屏(隐藏导航栏、状态栏,头条三面技术四面HR

本文介绍了Android设置Activity全屏的三种方法:通过manifest、style和代码实现,并详细讨论了如何在全面屏手机上隐藏导航栏。同时,针对谷歌官方的解决方案,解释了在不同场景下设置UI Flag的影响及注意事项,确保全屏效果在用户交互后仍能保持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

或者

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

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

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

requestWindowFeature(Window.FEATURE_NO_TITLE);

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

WindowManager.LayoutParams.FLAG_FULLSCREEN);

注意:

当有全面屏手机可以显示虚拟按键时,比如小米8手机,就会出现白色的虚拟按键区域,如图所示:

在这里插入图片描述

按照google的官方办法,设

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

置如下几个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一起使用会暂时隐藏导航栏。一旦用户与界面发生交互,导航栏又会出现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值