问题描述
我知道这个问题已经被问过很多次了,但所有的答案要么不起作用,要么使用了弃用的代码:
- Android Completely transparent Status Bar?
- Transparent status bar - before Android 4.4 (KitKat)
- Lollipop : draw behind statusBar with its color set to transparent
我想达到和最新的谷歌地图应用一样的效果:
- 完全透明的状态栏(只有状态栏。不是导航栏!)
- 未弃用的解决方案
WindowCompat.setDecorFitsSystemWindows(window,false)
部分工作,因为它还隐藏了导航栏
解决方法
第 1 步:要使状态栏透明:将以下内容添加到样式 themes.xml
或 sytles.xml
中:
<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
第 2 步:然后在活动中将状态栏与活动重叠:
使用的窗口标志从 API 级别 30 起已弃用,因此它们可以一直使用到 API 级别 29:
if (Build.VERSION.SDK_INT in 21..29) {
window.statusBarColor = Color.TRANSPARENT
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.decorView.systemUiVisibility =
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE
} else if (Build.VERSION.SDK_INT >= 30) {
window.statusBarColor = Color.TRANSPARENT
// Making status bar overlaps with the activity
WindowCompat.setDecorFitsSystemWindows(window,false)
}
这是在 API 级别 19 到 API 级别 30 的范围内在 8 个设备/模拟器上测试的。
,要使 StatusBar
完全透明,
首先,在主题中或通过代码将其颜色设置为透明:
//In Theme
<item name="android:statusBarColor">@android:color/transparent</item>
//In Code
window.statusBarColor = android.R.color.transparent
然后,要绘制 StatusBar
后面的视图,请使用以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
} else {
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
}
您还可以使用 boolean
属性 android:windowLightStatusBar
,它将 StatusBar
的文本和图标颜色设置为黑色或白色,您也可以以编程方式进行设置。如果您不想隐藏它,也可以为 NavigationBar
设置颜色。
输出:
,我通过以下方法获得了类似的结果。
<style name="Theme.WebImageDownloader" parent="Theme.MaterialComponents.DayNight.NoActionBar">
// make status bar icons colour grey
<item name="android:windowLightStatusBar">true</item>
// override the notch area
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
// make status bar transparent
<item name="android:windowTranslucentStatus">true</item>
// make navigation are transparent
<item name="android:windowTranslucentNavigation">true</item>
</style>
并在清单中设置上述主题
android:theme="@style/Theme.WebImageDownloader">
此外,我在活动中放置了我的工具栏,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/holo_blue_dark"
android:layout_height="match_parent">
// other views
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ToolBarStyle" />
</RelativeLayout>
有'ToolBarStyle',你可以为背景、文本等设置任何颜色
,private fun setStatusBar() {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT
window.navigationBarColor = Color.TRANSPARENT
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}