简体   繁体   中英

How do I get the bottom navigation bar height for Material Design 3?

I'm using a Material Design 3 bottom navigation bar .

<!-- activity_main.xml -->
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph"
        tools:ignore="FragmentTagUsage" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

I've added a floating action button (FAB) .

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/add_a_vehicle_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:contentDescription="@string/fab_content_description"
    app:srcCompat="@drawable/ic_baseline_add_24" />

However, the FAB is obscured behind the navigation bar.

带有模糊 FAB 的底部导航栏

I've tried to solving the issue by adding margin to the fragment following: How to get the ActionBar height? .

<fragment
    android:id="@+id/nav_host_fragment"
    ....
    android:layout_marginBottom="?android:attr/actionBarSize"
    ... />

However, that height is from the Material Design 2 bottom navigation bar and my FAB gets cut off because it's not enough margin.

带有 FAB 截断的底部导航栏

How do I obtain the height of the Material Design 3 bottom navigation bar so I can display my FAB correctly?

With Material3 the BottomNavigationView has a different minHeight .
You can apply something like:

   <fragment
        android:layout_marginBottom="@dimen/m3_bottom_nav_min_height"

在此处输入图像描述

Reference: android/material/bottomnavigation/res/values/dimens.xml#L32

The default height Navigation Bar in material2 is 56dp and you are using material3 where the default height is 80dp according to the official android documentation.

https://m3.material.io/components/navigation-bar/specs#8021e284-9f44-4ca8-859b-cc11635a86f3

Maybe you can try to get the height programmatically and set margin bottom dynamically during runtime?

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    navBar.getLayoutParams().height = resources.getDimensionPixelSize(resourceId);
}

Refer here and here for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM