我知道这个问题已经问过很多次了,但是所有的答案要么不起作用,要么使用不推荐的代码:
我想达到与最新谷歌地图应用程序相同的效果:

WindowCompat.setDecorFitsSystemWindows(window, false)部分工作,因为它还隐藏导航条
发布于 2021-07-22 23:43:03
步骤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-30的更新
这实际上并不能使状态栏透明,它使其半透明,而且仍然有阴影。
这在API-30上是正确的,因为设置<item name="android:windowTranslucentStatus">true</item>是合理的.
实际上,<item name="android:windowTranslucentStatus">true</item>只需要在API级别19上使用,如果您的应用程序大于这个级别,您可以完全拒绝它。
无论如何,解决这个问题的方法是覆盖API-30中的emes.xml/contes.xml;即有一个res\values-v30\themes.xml;您只需添加以下的主应用主题:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.TransparentStatusBar" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
</style>
</resources>用于API-30的更新2
刚刚在API 30上发现了一个错误,它的底部导航与隐藏其底部部分的活动重叠,这很可能是OP在使用地图时无法发现的。
要解决这个问题,根据文件:
您可以通过响应inset来解决重叠问题,inset指定屏幕中哪些部分与系统UI (如导航条或状态栏)相交。相交可以简单地显示在内容上方,但也可以通知应用程序系统手势。
因此,我们需要处理API级30+的系统条内嵌,以避免应用程序与导航栏重叠:
这需要活动布局的顶部根ViewGroup,因此需要适当地滑动LayoutParams。
这里我使用ConstraintLayout作为根布局,并使用FrameLayout.LayoutParams
/*
* Making the Navigation system bar not overlapping with the activity
*/
if (Build.VERSION.SDK_INT >= 30) {
// Root ViewGroup of my activity
val root = findViewById<ConstraintLayout>(R.id.root)
ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
// Apply the insets as a margin to the view. Here the system is setting
// only the bottom, left, and right dimensions, but apply whichever insets are
// appropriate to your layout. You can also update the view padding
// if that's more appropriate.
view.layoutParams = (view.layoutParams as FrameLayout.LayoutParams).apply {
leftMargin = insets.left
bottomMargin = insets.bottom
rightMargin = insets.right
}
// Return CONSUMED if you don't want want the window insets to keep being
// passed down to descendant views.
WindowInsetsCompat.CONSUMED
}
}这在API级别19到API级别30范围内的8个设备/模拟器上进行了测试。

发布于 2021-07-22 16:00:24
为了使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设置颜色。
输出:

发布于 2022-02-12 14:27:26
将此添加到主题中
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar" ns1:targetApi="23">true</item>并使用这种方法
private fun handleStatusBar() {
//set fullScreen (draw under statusBar and NavigationBar )
WindowCompat.setDecorFitsSystemWindows(window, false)
container.setOnApplyWindowInsetsListener { view, insets ->
val navigationBarHeight = WindowInsetsCompat.toWindowInsetsCompat(insets)
.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
view.updatePadding(bottom = navigationBarHeight)
WindowInsetsCompat.CONSUMED.toWindowInsets()
}
//make statusBar content dark
WindowCompat.getInsetsController(window, window.decorView)?.isAppearanceLightStatusBars =
true
window.statusBarColor = Color.TRANSPARENT
}https://stackoverflow.com/questions/68451704
复制相似问题