我搜索了文档,但只找到了这个:
链接。这是用来使酒吧半透明?我想做的是使状态栏完全透明(如下图所示),并使它向后兼容APK<19:
我的styles.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/ThemeActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/ThemeActionBar</item>
<item name="windowActionBarOverlay">true</item>
</style>
<style name="ThemeActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid">
<item name="android:background"> @null </item>
<!-- Support library compatibility -->
<item name="background">@null</item>
<item name="android:displayOptions"> showHome | useLogo</item>
<item name="displayOptions">showHome|useLogo</item>
</style>
</resources>
我能做的是:
有三个步骤:
1)只需使用此代码段到你的OnCreate方法
// FullScreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
如果你正在使用Fragment,你应该把这个代码段放在你活动的OnCreate方法中。
2)确保在/res/values-v21/styles.xml中设置透明度:
<item name="android:statusBarColor">@android:color/transparent</item>
或者你可以通过编程来设置透明度:
getWindow().setStatusBarColor(Color.TRANSPARENT);
3)无论如何你都应该在styles.xml中添加代码段
<item name="android:windowTranslucentStatus">true</item>
注意:此方法仅适用于API 21及以上版本。
这是一个简单的方法,我找到了很多搜索。
步骤1
在你的主题里放这个项目
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
步骤2
主要活动
WindowCompat.setDecorFitsSystemWindows(window, false)
非常重要,如果你使用底部导航栏
在一些设备中,API 30+ u会发现系统导航栏与底部导航栏重叠,如果在你的应用中使用它。
这就解决了这个问题。
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
}
}