[英]How do I get the height and width of the Android Navigation Bar programmatically?
[英]How do I get the bottom navigation bar height for Material Design 3?
我正在使用 Material Design 3底部導航欄。
<!-- 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>
我添加了一個浮動操作按鈕 (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" />
但是,FAB 隱藏在導航欄后面。
我試圖通過向以下片段添加邊距來解決問題: How to get the ActionBar height? .
<fragment
android:id="@+id/nav_host_fragment"
....
android:layout_marginBottom="?android:attr/actionBarSize"
... />
但是,該高度來自Material Design 2 底部導航欄,我的 FAB 被切斷,因為它沒有足夠的邊距。
如何獲得 Material Design 3 底部導航欄的高度,以便正確顯示我的 FAB?
對於 Material3, BottomNavigationView
具有不同的minHeight
。
你可以應用類似的東西:
<fragment
android:layout_marginBottom="@dimen/m3_bottom_nav_min_height"
參考: android/material/bottomnavigation/res/values/dimens.xml#L32
material2 中的默認高度導航欄為 56dp,根據官方 android 文檔,您使用的默認高度為 80dp 的 material3。
https://m3.material.io/components/navigation-bar/specs#8021e284-9f44-4ca8-859b-cc11635a86f3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.