gpt4 book ai didi

android - Actionbar 和 statusbar 之间的间隙,非半透明

转载 作者:行者123 更新时间:2023-11-30 05:00:11 24 4
gpt4 key购买 nike

我在状态栏和操作栏之间有一个间隙,就是无法让它消失。我玩过 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)

显示的 fragment 只是简单的 Constaintlayout,没有任何 android:fitsSystemWindows 属性。

关于如何消除这个差距的任何线索?

截图一: - - - - - - - - - - - - - - - - - - - - - 截图二:

主要 Activity 布局:

<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)

有了这个 View 布局

<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)
}

关于android - Actionbar 和 statusbar 之间的间隙,非半透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58389064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com