我如何使一个活动全屏?没有通知栏。
当前回答
在AndroidManifest.xml文件:
<activity
android:name=".Launch"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <!-- This line is important -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
或者在Java代码中:
protected void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
其他回答
从mozilla找到解决方案,他们在这里做了一个扩展库
如果链接中断,这里是代码
/**
* Retrieves a {@link WindowInsetsControllerCompat} for the top-level window decor view.
*/
fun Window.getWindowInsetsController(): WindowInsetsControllerCompat {
return WindowInsetsControllerCompat(this, this.decorView)
}
/**
* Attempts to call immersive mode using the View to hide the status bar and navigation buttons.
* @param onWindowFocusChangeListener optional callback to ensure immersive mode is stable
* Note that the callback reference should be kept by the caller and be used for [exitImmersiveModeIfNeeded] call.
*/
fun Activity.enterToImmersiveMode(
onWindowFocusChangeListener: ViewTreeObserver.OnWindowFocusChangeListener? = null
) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window.getWindowInsetsController().apply {
hide(WindowInsetsCompat.Type.systemBars())
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
// We need to make sure system bars do not become permanently visible after interactions with content
// see https://github.com/mozilla-mobile/fenix/issues/20240
onWindowFocusChangeListener?.let {
window.decorView.viewTreeObserver?.addOnWindowFocusChangeListener(it)
}
}
/**
* Attempts to come out from immersive mode using the View.
* @param onWindowFocusChangeListener optional callback to ensure immersive mode is stable
* Note that the callback reference should be kept by the caller and be the same used for [enterToImmersiveMode] call.
*/
@Suppress("DEPRECATION")
fun Activity.exitImmersiveModeIfNeeded(
onWindowFocusChangeListener: ViewTreeObserver.OnWindowFocusChangeListener? = null
) {
if (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON and window.attributes.flags == 0) {
// We left immersive mode already.
return
}
onWindowFocusChangeListener?.let {
window.decorView.viewTreeObserver?.removeOnWindowFocusChangeListener(it)
}
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window.getWindowInsetsController().apply {
show(WindowInsetsCompat.Type.systemBars())
}
}
在Android 10上,没有一款适合我。
但我工作得很好(oncreate的第一行):
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
setContentView(....);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
享受:)
通过缺口或切口区域显示内容。这可以从文档中得到帮助:
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES -在纵向和横向模式下将内容呈现到剪切区域。
对我来说最重要的是activity样式中的这一行:
// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
对我来说,我想以沉浸式模式展示图像。当我点击它时,我希望系统UI(状态和导航栏)显示出来。
以下是我的解决方案:
在Activity中,一些显示/隐藏系统UI的方法(状态条/导航条)
private fun hideSystemUI() {
sysUIHidden = true
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
)
}
private fun showSystemUI() {
sysUIHidden = false
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
)
}
2-确保在你的xml布局的根视图中
android:fitsSystemWindows="false"
3-全屏活动的风格将给状态/导航栏显示时一个半透明的背景:
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="android:statusBarColor">#50000000</item>
<item name="android:navigationBarColor">#50000000</item>
// Important to draw behind cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/sysTransparent</item>
</style>
充分沉浸:
private void askForFullScreen()
{
getActivity().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 // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
脱离完全沉浸模式:
private void moveOutOfFullScreen() {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
截至2022年
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.decorView.windowInsetsController?.hide(WindowInsets.Type.systemBars())
} else {
@Suppress("DEPRECATION") // Older API support
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?