首页 > 解决方案 > 如何制作fab center cut android studio

问题描述

一旦我听说如果你不能做某事 15 分钟寻求帮助......我已经尝试了 2 天。

现在我需要你的帮助。

我想在android studio的AppBar底部制作fab center

这对我不起作用链接 1 和此链接 2

进入屏幕

屏幕 2

屏幕 2 中的代码

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActionMenu">
    <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="@color/primaryDarkColor"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
    </com.google.android.material.appbar.AppBarLayout>
    <include
        android:id="@+id/include"
        layout="@layout/content_action_menu" />
    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar2"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primaryDarkColor"
        app:fabAttached="true"
        android:gravity="bottom"
        app:elevation="8dp"
        app:fabAlignmentMode="center"
        app:layout_anchor="@+id/include"
        app:layout_anchorGravity="bottom|center" />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        app:elevation="8dp"
        app:layout_anchor="@+id/bottomAppBar2"
        app:layout_anchorGravity="center|fill_vertical"
        app:srcCompat="@android:drawable/ic_menu_add" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

如何在 AppBar 底部制作工厂中心切割?

感谢关注

标签: androidandroid-studioandroid-layoutfloating-action-buttonandroid-bottomappbar

解决方案


我相信您正在寻找的代码是

app:fabCradleMargin="(x DP here)"

作为浮动操作按钮的属性。

这是我的一个项目的布局,它完全符合您的要求:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:layout_anchor="@+id/bottomAppBar"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/app_bar_menu"
        app:fabAnimationMode="slide"
        app:fabCradleRoundedCornerRadius="8dp"
        app:navigationIcon="@drawable/ic_settings"

        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:clickable="true"
        app:backgroundTint="@color/colorAccent"
        app:layout_anchor="@id/bottomAppBar"
        app:maxImageSize="40dp"
        app:srcCompat="@drawable/ic_scan"
        app:tint="@color/colorWhite" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

推荐阅读