繁体   English   中英

当 CoordinatorLayout 的宽度设置为 wrap_content 时,CoordinatorLayout 隐藏了一半的浮动操作按钮

[英]CoordinatorLayout is hiding half of the floating action button when CoordinatorLayout 's width is set to wrap_content

所以我正在尝试制作这样的布局:

使用 FramLayout 来保存片段,我希望 frameLayout 位于 topAppBar 下方和 floatingActionButton 上方,但我无法使其工作

在此处输入图像描述

我试图制作一个 relativeLayout 并将 FrameLayout 放在 topApbar 和 coordinatorAyout 之间,但它会切断 FAB 并且 fab 下的圆形空间消失了,如下图所示

在此处输入图像描述

这是我的activity_main.xml:

<?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:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/AppBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topToolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary"
            app:layout_anchor="@+id/AppBarLayout"
            app:layout_anchorGravity="center"
            app:menu="@menu/top_nav_menu"
            app:title="Reading List">

        </com.google.android.material.appbar.MaterialToolbar>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/topToolbar"
            android:layout_above="@id/coordinatorLayout"
            android:background="@color/purple_500"/>
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/coordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            tools:context=".MainActivity">


            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/secondary"
                android:src="@drawable/ic_add"
                app:layout_anchor="@id/bottomAppBar"
                app:maxImageSize="55dp"
                app:tint="@color/white" />

            <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/primary">

                <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:elevation="0dp"
                    app:itemIconSize="27dp"
                    app:itemIconTint="@color/white"
                    app:itemTextColor="@color/white"
                    app:menu="@menu/bottom_nav_menu">

                </com.google.android.material.bottomnavigation.BottomNavigationView>

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


        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </RelativeLayout>


    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:backgroundTint="@color/primary"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/sidebar_header"
        app:menu="@menu/nav_drawer_menu"


        ></com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>



试试这个(我将所有元素移到CoordinatorLayout并添加app:layout_behavior="@string/appbar_scrolling_view_behavior"到您的FrameLayout ):

<?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:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        tools:context=".MainActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/AppBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/primary"
                app:layout_anchor="@+id/AppBarLayout"
                app:layout_anchorGravity="center"
                app:menu="@menu/top_nav_menu"
                app:title="Reading List" />

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/coordinatorLayout"
            android:layout_below="@id/topToolbar"
            android:background="@color/purple_500" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/secondary"
            android:src="@drawable/ic_add"
            app:layout_anchor="@id/bottomAppBar"
            app:maxImageSize="55dp"
            app:tint="@color/white" />

        <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/primary">

            <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:elevation="0dp"
                app:itemIconSize="27dp"
                app:itemIconTint="@color/white"
                app:itemTextColor="@color/white"
                app:menu="@menu/bottom_nav_menu">

            </com.google.android.material.bottomnavigation.BottomNavigationView>

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

    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:backgroundTint="@color/primary"
        app:headerLayout="@layout/sidebar_header"
        app:menu="@menu/nav_drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM