我有一个活动,其中有BottomNavigationView
。我为每个ButtomNavigationView
项目分别制作了片段。我想为其中一个片段(HomeFragment
)提供一个透明的状态栏,并为其余的片段提供普通的ActionBar。
因此,我正在HomeFragment
中设置透明状态栏,如下所示
fun enableFullScreen(activity: Activity){
if ((19 until 21).contains(Build.VERSION.SDK_INT)) {
setWindowFlag(activity, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true)
}
if (Build.VERSION.SDK_INT >= 19) {
activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
if (Build.VERSION.SDK_INT >= 21) {
setWindowFlag(activity, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false)
activity.window.statusBarColor = Color.TRANSPARENT
}
}
private fun setWindowFlag(activity: Activity, bits: Int, on: Boolean) {
val win = activity.window
val winParams = win.attributes
if (on) {
winParams.flags = winParams.flags or bits
} else {
winParams.flags = winParams.flags and bits.inv()
}
win.attributes = winParams
}
在其他片段中,我进入了如下的正常状态
fun disableFullScreen(activity: Activity){
if (Build.VERSION.SDK_INT >= 19) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
}
if (Build.VERSION.SDK_INT >= 23){
activity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
}
我正在实现我打算做的事情,但是有一个小错误我无法弄清楚。
当我转到HomeFragment
时,这是一个透明的状态栏,BottomNavigationView
向上移动并返回其原始位置。有时它只是停留在上面,如果我再次单击Home
按钮,它就会消失。
供参考的是我的activity_main
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:itemTextColor="@color/green"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我做错了什么?还是有更好的方法来做我打算做的事?