我如何使一个活动全屏?没有通知栏。
当前回答
内部styles.xml……
<!-- No action bar -->
<style name="NoActonBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme customization. -->
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#444</item>
<item name="colorAccent">#999</item>
<item name="android:windowFullscreen">true</item>
</style>
这对我很管用。希望对你有所帮助。
其他回答
@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);
}
}
小心
requestWindowFeature(Window.FEATURE_NO_TITLE);
如果您正在使用任何方法设置操作栏如下:
getSupportActionBar().setHomeButtonEnabled(true);
它将导致一个空指针异常。
内部styles.xml……
<!-- No action bar -->
<style name="NoActonBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme customization. -->
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#444</item>
<item name="colorAccent">#999</item>
<item name="android:windowFullscreen">true</item>
</style>
这对我很管用。希望对你有所帮助。
对于安卓X
1. 透明状态栏
window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.statusBarColor = Color.TRANSPARENT
2. 透明的状态栏和底部导航栏
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
);
3.隐藏状态栏
API 30+推荐的Compat解决方案(包括向后兼容性)
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView) ?: return
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars())
https://developer.android.com/training/system-ui/immersive
4. 隐藏状态栏和底部导航栏
消防级别30:
SystemUiVisibility标志已弃用。请改用WindowInsetsController。
和3一样。,只需使用WindowInsetsCompat.Type.systemBars()
val actionBar: ActionBar? = supportActionBar
if (actionBar != null) actionBar.hide()
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView) ?: return
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
https://developer.android.com/training/system-ui/immersive
把这些代码放在哪里?
override fun onCreate(savedInstanceState: Bundle?) {
/* Put above code here ..... */
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_slider)
}
Note
我在Pixel 3A模拟器中检查了这段代码 可能自定义android操作系统不支持 set style <style name="主题。"全屏Theme.MaterialComponents.DayNight.NoActionBar“父= >
根据安卓开发者培训
你应该做到以下几点:
科特林:
private fun hideSystemBars() {
val windowInsetsController =
ViewCompat.getWindowInsetsController(window.decorView) ?: return
// Configure the behavior of the hidden system bars
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
Java:
private void hideSystemBars() {
WindowInsetsControllerCompat windowInsetsController =
ViewCompat.getWindowInsetsController(getWindow().getDecorView());
if (windowInsetsController == null) {
return;
}
// Configure the behavior of the hidden system bars
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Hide both the status bar and the navigation bar
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
}
推荐文章
- Manifest合并失败:uses-sdk:minSdkVersion 14
- 为什么Android工作室说“等待调试器”如果我不调试?
- 如何检查我的EditText字段是否为空?
- Android从图库中选择图像
- 后台任务,进度对话框,方向改变-有任何100%工作的解决方案吗?
- Android:垂直对齐多行EditText(文本区域)
- Android无尽列表
- Android room persistent: AppDatabase_Impl不存在
- 错误:执行失败的任务':app:compileDebugKotlin'。>编译错误。详细信息请参见日志
- 在Android中使用URI生成器或使用变量创建URL
- 缩放图像以填充ImageView宽度并保持纵横比
- 列表视图的自定义适配器
- 在Android中设置TextView span的颜色
- 如何以编程方式在RelativeLayout中布局视图?
- Android Facebook集成无效键散列