【问题标题】:My floating action button is not displayed properly...?我的浮动操作按钮未正确显示...?
【发布时间】:2021-03-22 07:20:39
【问题描述】:

我使用 TAB LAYOUTView Pager 和一个 BOTTOM APP BAR 与底部导航的浮动操作按钮,但我认为我的浮动操作按钮显示在视图寻呼机下,所以它看起来像是从上面切下来的......请帮我解决这个问题

这是我的 XML 代码....

<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"
    android:background="@drawable/ic_main_bg"
    tools:context=".home.HomeActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bottom_navigation_bg"
            app:fabCradleRoundedCornerRadius="0dp"
            app:backgroundTint="@color/color_primary"
            />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:backgroundTint="@color/color_primary"
            app:srcCompat="@drawable/ic_plus"
            app:layout_anchor="@id/bottomAppBar"
            android:contentDescription="@string/add_item_to_this_destination" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_primary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/color_secondary"
        app:tabSelectedTextColor="@color/color_secondary"
        app:tabTextColor="@color/color_secondary" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/coordinator_layout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tab_layout">

    </androidx.viewpager2.widget.ViewPager2>

</androidx.constraintlayout.widget.ConstraintLayout>

这就是它的样子

enter image description

【问题讨论】:

    标签: android floating-action-button bottomnavigationview android-bottomappbar


    【解决方案1】:

    问题是因为 CoordinatorLayout

    解决方案 1: 添加

    android:clipToPadding="false"
    android:clipChildren="false"
    

    在协调器布局中

    解决方案 2: 将 CoordinatorLayout 设为父布局,并将所有子布局放入其中。

    解决方案 3:

    在 view_pager_2 中 添加

                android:layout_marginBottom="?actionBarSize"
    

    解决方案 4: 将此行添加到您的工厂

    android:elevation="10dp"
    

    【讨论】:

    • yeahh..我知道你上面提到的解决方案2,但我试图在约束布局中进行这项工作
    • 是的,你的解决方案 2 完全有效
    • 很高兴它有帮助。请接受它作为答案。
    • @AnkitVerma 我又添加了一个解决方案。请也检查一下。
    【解决方案2】:

    将以下代码添加到您的 BootomAppBar 标签:

        app:fabAlignmentMode="center"
        app:fabCradleMargin="8dp"
    

    所以会变成:

     <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bottom_navigation_bg"
            app:fabCradleRoundedCornerRadius="0dp"
            app:backgroundTint="@color/color_primary"
            app:fabAlignmentMode="center"
            app:fabCradleMargin="8dp"
            />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-11
      • 2016-11-09
      • 1970-01-01
      • 2022-06-25
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多