【问题标题】:Arrange a BottomNavigationView, a FAB, and a FrameLayout in a CoordinatorLayout and make it work with Snackbar在 CoordinatorLayout 中安排一个 BottomNavigationView、一个 FAB 和一个 FrameLayout 并使其与 Snackbar 一起使用
【发布时间】:2020-11-19 19:14:02
【问题描述】:

我想在CoordinatorLayout 中放置一个FrameLayout 用于托管Fragments、一个BottomNavigationView 和一个FAB。我想在BottomNavigationViewFAB 上显示Snackbar 以上下移动以适应Snackbar。我想出了以下布局,但无法正确获取 Snackbar 和 FAB 行为。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".landingpage.MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/content_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/fragment_cont"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_main_activity_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/extended_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="72dp"
    android:contentDescription="@string/cont_desc"
    android:text="@string/chat"
    app:icon="@drawable/ic_baseline_chat_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

如果我在 post 中设置 LayoutParams,则该边距 72dp 边距也会显示在布局中以向上抛出 FAB 方式。如果我不使用边距,FAB 将堆叠在BottomNavigationView 上。我可以使用addCallback 以编程方式设置 FAB 边距。然后它也会跳跃,然后将自己对齐到正确的位置。总体而言,在用户体验方面,这不是一个好的体验。

【问题讨论】:

    标签: android floating-action-button android-coordinatorlayout bottomnavigationview material-components-android


    【解决方案1】:

    我想在BottomNavigationView 上显示Snackbar 和 FAB 上下移动以适应Snackbar。我来了 使用以下布局,但我无法获得 Snackbar 和 FAB 行为正确。

    你得到了一个不好的行为,因为你试图通过使用 BottomNavigationViewFAB 来实现一些不常见或可能没有以这种方式实现的东西一起。实际上,在布局的底部使用FABBottomAppBar 更为常见。


    然后它也会跳跃,然后将自己对齐到正确的位置。 总体而言,在用户体验方面,这不是一个好的体验。

    尽管从用户体验的角度来看,这对用户来说并不是一个好的体验,但是我可能会建议以下方法:

    最佳方法:在LinearLayout 中使用另一个CoordinatorLayout(支持FAB 动画):

    代码:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:title="Test" />
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.core.widget.NestedScrollView
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <FrameLayout
                android:id="@+id/fragment_cont"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </androidx.core.widget.NestedScrollView>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:id="@+id/SnackBar"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                    android:id="@+id/extended_fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="16dp"
                    android:text="Chat"
                    android:textColor="@color/white"
                    app:icon="@drawable/ic_baseline_send_24"
                    app:iconTint="@color/white" />
    
            </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_insetEdge="bottom"
                app:menu="@menu/escrow_menu" />
        </LinearLayout>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    在 Kotlin 方面:

    extended_fab.setOnClickListener {
    
                val snack: Snackbar = Snackbar.make(SnackBar, " Successfully ...!", Snackbar.LENGTH_SHORT)
                snack.show()
            }
    

    2。 BottomAppBar & FAB & setAnchorView() 没有FAB 动画的方法

    • 锚定到 FAB

    代码:

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <androidx.core.widget.NestedScrollView
            android:id="@+id/content_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <!-- your FrameLayout maybe -->
    
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="SHOW SnakeBar"
                app:layout_anchor="@+id/content_layout"
                app:layout_anchorGravity="center" />
    
    
        </androidx.core.widget.NestedScrollView>
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:title="Test" />
        </com.google.android.material.appbar.AppBarLayout>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            app:layout_anchor="@id/bar" />
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:backgroundTint="@color/colorPrimaryDark">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:drawableTop="@drawable/ic_baseline_send_24"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:text="Personal"
                    android:textColor="#FFFFFF">
    
                </TextView>
    
                <TextView
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:drawableTop="@drawable/ic_baseline_send_24"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:text="Personal"
                    android:textColor="#FFFFFF">
    
                </TextView>
    
                <TextView
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:drawableTop="@drawable/ic_baseline_send_24"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:textColor="#FFFFFF"
                    android:visibility="invisible">
    
                </TextView>
    
                <TextView
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:drawableTop="@drawable/ic_baseline_send_24"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:text="Personal"
                    android:textColor="#FFFFFF">
    
                </TextView>
    
                <TextView
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="?android:attr/selectableItemBackground"
                    android:drawableTop="@drawable/ic_baseline_send_24"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:text="Personal"
                    android:textColor="#FFFFFF">
    
                </TextView>
    
            </LinearLayout>
    
        </com.google.android.material.bottomappbar.BottomAppBar>
    
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    3.使用 FAB - BottomNavigationView - ConstraintLayout 不使用 FAB 动画

    代码:

    <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/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floating_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_margin="16dp"
            app:backgroundTint="@color/colorPrimary"
            app:elevation="10dp"
            app:layout_constraintBottom_toTopOf="@id/navigation"
            app:layout_constraintLeft_toRightOf="@id/navigation"
            app:layout_constraintRight_toLeftOf="@id/navigation"
            app:layout_constraintTop_toBottomOf="@id/navigation"
            app:layout_constraintTop_toTopOf="@id/navigation"
            app:layout_insetEdge="bottom" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark"
            android:visibility="visible"
            app:itemIconTint="@color/white"
            app:itemTextColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/escrow_menu" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="290dp"
            android:layout_marginBottom="485dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 最好的方法有一个问题。 NestedScrollView 位于包含 BottomNavigationView 的 LinearLayout 下。必须使用 SnackBar 布局,但如果您从片段显示小吃栏,那么它仍会显示在底部导航视图上。这是答案的最大限制之一。
    • 临时解决方案是从片段中调用活动方法并添加 56dp 的底部边距。我不确定是否有更好的解决方案。
    猜你喜欢
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2015-05-09
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2019-09-29
    相关资源
    最近更新 更多