FloatingButton 在 Android Studio 中不居中

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

我正在 Android Studio 中制作一个包含底部导航的屏幕,由教程指导,但就我而言,出现了错误,我无法解决它。

正如您在图像中看到的,左侧的 2 个项目和右侧的 2 个项目之间有一个空格,这正是我添加

app:layout_anchor="@id/bottom_navigation"
时浮动按钮所在的位置,它与右下角,如下图所示:

Activity_drawer(主要):

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    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:orientation="vertical"

    android:id="@+id/drawer_layout"
    tools:context=".Drawer">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:id="@+id/toolbar"
            android:elevation="4dp"
            android:background="@color/pinks"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/fragment_container"/>

        <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"
            android:backgroundTint="@color/pinks"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="50dp">

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_navigation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/bottom_menu" />

        </com.google.android.material.bottomappbar.BottomAppBar>

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/pinks"
                android:src="@drawable/nav_add"
                android:tint="@color/pink"
                app:layout_anchor="@id/bottom_navigation"
                app:maxImageSize="40dp" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        android:background="@color/pink"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        android:theme="@style/ThemeOverlay.AppCompat.navTheme"/>

</androidx.drawerlayout.widget.DrawerLayout>

底部菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/bottom_home"
        android:title="Início"
        android:icon="@drawable/bottom_home" />

    <item
        android:id="@+id/bottom_perguntas"
        android:title="Perguntas"
        android:icon="@drawable/bottom_question" />
    <item

        android:title=""
        android:enabled="true" />
    <item
        android:id="@+id/bottom_settings"
        android:title="Configurações"
        android:icon="@drawable/bottom_settings" />

    <item
        android:id="@+id/bottom_profile"
        android:title="Conta"
        android:icon="@drawable/bottom_profile" />

</menu>

我已经尝试过使用一些帖子中的一些方法,比如在 FloatingButton 上添加

app:fabAlignmentMode="center"
,但它仍然在右侧。

按钮应如下所示:

android mobile floating-action-button
1个回答
0
投票

请参考此代码。它将帮助您添加带有底部导航栏的浮动按钮。

<?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=".MainActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fabCradleMargin="10dp"
        app:fabCradleVerticalOffset="10dp"
        app:fabCradleRoundedCornerRadius="20dp"
        android:layout_gravity="bottom">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="16dp"
            android:background="@android:color/transparent"
            app:menu="@menu/bottom_nav_menu" />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/bottomAppBar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
© www.soinside.com 2019 - 2024. All rights reserved.