我搜索了文档,但只找到了这个:
链接。这是用来使酒吧半透明?我想做的是使状态栏完全透明(如下图所示),并使它向后兼容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>
我能做的是:
用于谷歌的最后一次更新。这是我的代码。这将是透明的状态栏和导航栏:
把你的主题改为:
<style name="Theme.Pink"
parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/pink_primary</item>
<item name="colorPrimaryVariant">@color/pink_dark</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/pink_light</item>
<item name="colorSecondaryVariant">@color/pink_primary</item>
<item name="colorOnSecondary">@color/black</item>
<item name="android:textColor">?attr/colorOnPrimary</item>
<!-- Status bar color. -->
<!-- Customize your theme here. -->
//transparent statusbar
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowIsTranslucent">true</item>
//transparent navigationbar
<item name="android:windowTranslucentNavigation">true</item>
</style>
然后在layout (file xml)中:添加2个属性point_top和point_bottom来确定应用程序其他组件的边距值的位置:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_activity">
<TextView
android:id="@+id/point_top"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/point_bottom"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<.....>
</androidx.constraintlayout.widget.ConstraintLayout>
然后在baseActivity或yourActivity中:在onCreate中添加以下代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = setLayout()
//set inset for view
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _, insets ->
val paramsTop =
findViewById<TextView>(R.id.point_top).layoutParams as MarginLayoutParams
paramsTop.setMargins(0, insets.getInsets(WindowInsetsCompat.Type.systemBars()).top, 0, 0)
findViewById<TextView>(R.id.point_top).layoutParams = paramsTop
val paramsBottom =
findViewById<TextView>(R.id.point_bottom).layoutParams as MarginLayoutParams
paramsBottom.setMargins(0, 0, 0, insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom)
findViewById<TextView>(R.id.point_bottom).layoutParams = paramsBottom
insets.consumeSystemWindowInsets()
}
setContentView(binding.root)
}
希望这对大家有所帮助。
这是一个简单的方法,我找到了很多搜索。
步骤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
}
}