浮动操作按钮拒绝停留在中心

问题描述 投票:0回答:1

我目前正在尝试制作一个地理跟踪应用程序,但是现在我陷入了尝试实现菜单的困境。我看到了许多不同的实现,但最终还是使用了底部导航栏 最终结果看起来像这样

但是,所有教程和帮助页面都使用一个名为“底部应用程序栏”的组件,该组件需要在协调器布局内使用。

当我使用谷歌地图片段作为我的主要对象来查看时,我将它放在约束布局内。所以我解决了这个问题,我在地图片段下的约束布局中放置了一个协调器布局,然后添加了其他所有内容。

这一切都很好,直到最后的添加按钮应该位于中心并且看起来很漂亮,但它只是拒绝进入中心并停留在右下角。

在搞乱它时,大多数(如果不是所有)教程最后都会使用移动图标/浮动操作按钮

app:layout_achnor="@id/bottom app bar"
(它只是一个底部应用栏,可以随意命名)

当我应用这条线时,它只是卡在右下角,并且拒绝留在中心,无论与其他教程显示的不同。它看起来像这样

我似乎找不到其他人遇到此解决方案,我尝试搜索未居中或对齐的浮动操作按钮,他们都提出了未居中于按钮的图像或图标,这不是我的问题。

我对 android studio 还很陌生,这肯定与我的布局中的某些内容或我如何完成某些组件的布局有关,但我似乎无法弄清楚。

我目前通过手动指定保证金限制来解决这个问题,如下所示

有什么办法可以像我想要的那样得到它吗?

这是我的代码

<?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/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.widget.SearchView
        android:id="@+id/mapSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:queryHint="Search . . ."
        android:iconifiedByDefault="false"
        android:elevation="5dp"
        android:background = "@drawable/bg_search_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="50dp"
            >
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bottomNavigationView"
                android:layout_marginRight="20dp"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/bottom_navigation_menu"
                android:background="@drawable/transparent_background"
                />
        </com.google.android.material.bottomappbar.BottomAppBar>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floating_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            android:src="@drawable/baseline_add_24"
            android:layout_gravity="bottom|center"
            android:layout_marginBottom="15dp"/>
<!--            app:layout_anchorGravity="center"-->
<!--            app:layout_anchor="@id/bottomAppBar"-->
<!--            />-->
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
android-studio layout floating-action-button
1个回答
0
投票

没关系,我已经弄清楚了,这与我的主题有关

在我的主题中,他们在 res/values/theme.xml 下设置的默认主题是这样的

<style name="Base.Theme.Coursework2" parent="Theme.Material3.DayNight.NoActionBar">

但是请确保您使用的是这个

<style name="Base.Theme.Coursework2" parent="Theme.MaterialComponents">

在我的浮动操作栏中我的代码是这样的

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:src="@drawable/baseline_add_24"
        app:layout_anchor="@id/bottomAppBar"
        />
© www.soinside.com 2019 - 2024. All rights reserved.