我如何使一个活动全屏?没有通知栏。
当前回答
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
adjustFullScreen(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
adjustFullScreen(getResources().getConfiguration());
}
}
private void adjustFullScreen(Configuration config) {
final View decorView = getWindow().getDecorView();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
decorView.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);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
其他回答
奇巧有一种叫做沉浸式全屏模式的技术。
例子
从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())
}
}
这些答案大部分似乎都过时了。Developer.android.com建议这样做:
科特林:
private fun hideSystemBars() {
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView) ?: return
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
Java:
private void hideSystemBars() {
WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
windowInsetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
更多信息:https://developer.android.com/training/system-ui/immersive
你可以通过编程来实现:
public class ActivityName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
或者你也可以通过AndroidManifest.xml文件来实现:
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
编辑:
如果你正在使用AppCompatActivity,那么你需要添加新的主题
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
然后使用它。
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>
感谢https://stackoverflow.com/a/25365193/1646479
截至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)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?