android 底部导航顶部的FAB

yhived7q  于 2023-05-21  发布在  Android
关注(0)|答案(2)|浏览(199)

如何在底部导航上显示晶圆厂?这就是我所尝试的:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/trendingPosts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/trendingPosts"
        android:focusedByDefault="true"
        android:focusable="true"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/trendingPosts"
        app:layout_constraintTop_toBottomOf="@+id/trendingPosts"
        app:menu="@menu/main_menu"
        android:focusable="false"
        android:focusedByDefault="false"/>

但是,晶圆厂就在它下面。查看图片:

vecaoik1

vecaoik11#

您需要使用协调器布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="@+id/trendingPosts"
                app:layout_constraintTop_toBottomOf="@+id/trendingPosts"
                app:menu="@menu/main_menu"
                android:focusable="false"
                android:focusedByDefault="false"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

       <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_gravity="bottom|center"
            android:focusedByDefault="true"
            android:focusable="true"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
tp5buhyn

tp5buhyn2#

一个很好的方法是在BottomNavigationView和FloatingActionButton中使用elevation参数,并将fab按钮的elevation比BottomNavigationView多,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout  
xmlns:app="http://schemas.android.com/apk/res-auto"      
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav_instructor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/white"
    android:elevation="15dp"
    app:itemIconSize="18dp"
    app:itemTextColor="@color/light_grey_text_color"
    style="@style/Theme.OpenInApp"
    app:itemIconTint="@color/light_grey_text_color"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/home_bottom_nav">

</com.google.android.material.bottomnavigation.BottomNavigationView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_marginBottom="25dp"
    android:layout_gravity="bottom|right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/add"
    app:elevation="20dp"
    app:layout_constraintBottom_toBottomOf="@+id/bottom_nav_instructor"
    app:layout_constraintEnd_toEndOf="@+id/bottom_nav_instructor"
    app:layout_constraintStart_toStartOf="@+id/bottom_nav_instructor" />

 </androidx.constraintlayout.widget.ConstraintLayout>

相关问题