首页 > 解决方案 > Android材质组件底部app栏截断

问题描述

我已经尝试实现材料组件底部应用栏,遵循这些指南并对 AndroidX 进行重构 + 更新我的 AppTheme。 材质组件 - 底部应用栏

到目前为止一切顺利,一切正常,但按钮在我的片段中被切断。 应用程序运行时的屏幕截图

然而,xml 预览显示了这一点,看起来一切都很好: 在此处输入图像描述

这是我的xml代码:

<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"
android:fitsSystemWindows="true">

<!-- Other components and views -->

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottombar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:elevation="2dp"
    app:fabAlignmentMode="center"
    app:fabCradleVerticalOffset="10dp"
    app:fabCradleMargin="10dp" />


<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|center_horizontal"
    android:adjustViewBounds="true"
    app:layout_anchor="@id/bottombar"
    app:layout_anchorGravity="top|center"
    app:srcCompat="@drawable/marker" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我什至增加了fabCradleMarginfabCradleOffset- 否则按钮完全在底部,而不是像它应该的那样漂浮在那个半圆中......

有人对此有任何线索吗?非常感谢!

标签: androidandroid-layoutmaterial-designfloating-action-buttonandroid-bottomappbar

解决方案


所以我注意到它与底部应用栏的高度有关。如果我手动将其设置为80dp,则 Fab 将按预期显示。

我尝试了更多,并注意到此手动高度设置仅在片段中是必需的。无论如何,我只将底部的应用栏放在一个片段中进行测试。所以现在我以与我的指南相同的方式实现它MainActivity,然后设置一个函数showFab(Boolean enable),然后可以根据需要在不同的片段中调用它。

如果有人面临同样的问题,它就像一个魅力。我想这可能是因为它不应该只在单个片段中实现。


推荐阅读