如何获得 Material 设计 3 的底部导航栏高度?
How do I get the bottom navigation bar height for Material Design 3?
我使用 Material 设计 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>
我添加了一个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" />
但是,FAB 在导航栏后面被遮挡了。
我试图通过向以下片段添加边距来解决问题:How to get the ActionBar height?.
<fragment
android:id="@+id/nav_host_fragment"
....
android:layout_marginBottom="?android:attr/actionBarSize"
... />
但是,该高度来自 Material Design 2 bottom navigation bar,我的 FAB 被切断了,因为它没有足够的边距。
如何获取 Material Design 3 底部导航栏的高度以便正确显示我的 FAB?
也许您可以尝试以编程方式获取高度并在运行时动态设置边距底部?
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navBar.getLayoutParams().height = resources.getDimensionPixelSize(resourceId);
}
参考here and here了解更多信息。
对于 Material3,BottomNavigationView
具有不同的 minHeight
。
您可以应用类似:
<fragment
android:layout_marginBottom="@dimen/m3_bottom_nav_min_height"
参考:android/material/bottomnavigation/res/values/dimens.xml#L32
我使用 Material 设计 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>
我添加了一个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" />
但是,FAB 在导航栏后面被遮挡了。
我试图通过向以下片段添加边距来解决问题:How to get the ActionBar height?.
<fragment
android:id="@+id/nav_host_fragment"
....
android:layout_marginBottom="?android:attr/actionBarSize"
... />
但是,该高度来自 Material Design 2 bottom navigation bar,我的 FAB 被切断了,因为它没有足够的边距。
如何获取 Material Design 3 底部导航栏的高度以便正确显示我的 FAB?
也许您可以尝试以编程方式获取高度并在运行时动态设置边距底部?
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
navBar.getLayoutParams().height = resources.getDimensionPixelSize(resourceId);
}
参考here and here了解更多信息。
对于 Material3,BottomNavigationView
具有不同的 minHeight
。
您可以应用类似:
<fragment
android:layout_marginBottom="@dimen/m3_bottom_nav_min_height"
参考:android/material/bottomnavigation/res/values/dimens.xml#L32