我在状态栏和操作栏之间留有缝隙,只是无法消除它。我玩过android:fitsSystemWindows,下面您将看到它的示例。我通过使用AppCompatActivity使用supportActionBar。它是使用导航组件设置的:
val navController = navController()
val appBarConfiguration = AppBarConfiguration(setOf(R.id.navigation_onboarding, R.id.projectsFragment, R.id.navigation_menu))
setupActionBarWithNavController(navController, appBarConfiguration)
显示的片段只是一个Constaintlayout,没有任何android:fitsSystemWindows属性。
关于如何消除这一差距的任何线索?
屏幕截图1:----------------------屏幕截图2:
主要活动布局:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:fitsSystemWindows="true" <!--Screenshot 1-->
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/navigationHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:fitsSystemWindows="true" <!--Screenshot 2-->
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:visibility="gone"
app:itemIconTint="@color/bottom_navigation"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
主题:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
...
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
我们最终通过添加自定义工具栏解决了它,并启用了半透明状态栏:
toolbar.adjustHeightUnderStatusBar(this)
setSupportActionBar(toolbar)
使用此视图布局
<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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/grey"
android:elevation="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title=""
app:titleTextColor="@color/darkBlue"
tools:ignore="HardcodedText" />
<fragment
android:id="@+id/navigationHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
android:visibility="gone"
app:itemIconTint="@color/bottom_navigation"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
adjustHeightUnderStatusBar函数:
fun Toolbar.adjustHeightUnderStatusBar(activity: BaseActivity) {
val statusBarHeight = 24.px
var actionBarHeight = 48.px
val value = TypedValue()
if (activity.theme.resolveAttribute(android.R.attr.actionBarSize, value, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(value.data, resources.displayMetrics)
}
val layoutParams = layoutParams as ConstraintLayout.LayoutParams
layoutParams.height = actionBarHeight + statusBarHeight
this.layoutParams = layoutParams
setTopPadding(statusBarHeight)
}