我如何使一个活动全屏?没有通知栏。
当前回答
在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);
}
其他回答
在onCreate()中的setContentView之后使用这个方法,并通过getWindow()传递Window对象。
public void makeActivityFullScreen(Window window){
View decorView = window.getDecorView();
// int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
);
}
此代码也将适用于缺口屏幕。要检查缺口全屏,你需要安卓P,但如果你有一个缺口显示手机,然后去设置->显示设置->应用程序显示比例->选择你的应用程序->有两个安全的选项是显示和全屏,请选择全屏并运行应用程序,你可以看到缺口的全屏也没有安卓派
谢谢你的回答@克里斯蒂安,我得到了错误
android.util.AndroidRuntimeException:必须调用requestFeature() 添加内容前
我用
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
-----
-----
}
通过缺口或切口区域显示内容。这可以从文档中得到帮助:
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>
从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())
}
}
创建一个空活动,并在onCreate中添加两行。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// full screen activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}
...
}
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。