android - FAB 不 float

标签 android layout floating-action-button android-bottomappbar

Activity 布局有点复杂...它是一个 fragment Activity,底部 fragment ('bottomBarFragmentPlaceholder')是 FAB 放在中间的 bottomAppBar。

Activity 布局如下:

<?xml version="1.0" encoding="utf-8"?><!-- practice_activity.xml -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/progressBarTimeText"
                android:layout_alignBottom="@id/progressBarTimeText"
                android:scaleY="6" />

            <TextView
                android:id="@+id/progressBarTimeText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="#00000000"
                android:gravity="center" />
        </RelativeLayout>

        <androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <FrameLayout
            android:id="@+id/bottomBarFragmentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</FrameLayout>

那么BottomAppBar布局如下:

<?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"
    android:id="@+id/bottomBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/primaryLightColor"
        android:backgroundTint="@color/primaryLightColor"
        app:fabAlignmentMode="center"
        app:menu="@drawable/ic_action_overflow"
        app:navigationIcon="@drawable/ic_menu_24"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
        app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/baseline_publish_black_24dp"
            app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

And the outcome is:

为什么我的 FAB 被推下而不是放入支架中?

非常感谢任何帮助。

谢谢, 国标。

最佳答案

好吧,尝试不同的布局,直到我终于把它全部设置好......

关键见解:

  • 在上层不需要允许覆盖功能( float 按钮)所需的 FrameLayout,但在可能的最低层只需要...(请参阅下面更新的布局)

  • bottomAppBar 布局不能与 layout_height="wrap_content",它需要被赋予占用父级所有空间的能力...("match_parent"),这就是 FAB 将被赋予足够高度的方式正确放置在 Gradle 中。

最后,这里是更新后的布局:

Fragment Activity布局如下:

<?xml version="1.0" encoding="utf-8"?><!-- practice_activity.xml -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/progressBarTimeText"
            android:layout_alignBottom="@id/progressBarTimeText"
            android:scaleY="6" />

        <TextView
            android:id="@+id/progressBarTimeText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="#00000000"
            android:gravity="center" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_gravity="center|fill_vertical"
            android:layout_height="match_parent" />

        <FrameLayout
            android:id="@+id/bottomBarFragmentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"/>
    </FrameLayout>
</LinearLayout>

BottomAppBar布局如下:

<?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"
    android:id="@+id/bottomBarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/primaryLightColor"
        android:backgroundTint="@color/primaryLightColor"
        app:fabAlignmentMode="center"
        app:menu="@drawable/ic_action_overflow"
        app:navigationIcon="@drawable/ic_menu_24"
        app:popupTheme="@style/ThemeOverlay.MaterialComponents.Dark"
        app:theme="@style/ThemeOverlay.MaterialComponents.ActionBar" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/baseline_publish_black_24dp"
            app:layout_anchor="@id/bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我希望这可以帮助将来遇到类似问题的其他人...

快乐的编码... 国标。

关于android - FAB 不 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54035953/

相关文章:

android - 使FAB不被夹在底部导航栏内

Android ArrayList迭代

java - 如何在 ArrayAdapter 中使用带有 4 个不同单选按钮的 Radiogroup

android - TextField 文本字段在 QML 上不返回值 | QT 5.12

html - 设置元素的宽度严格等于它的文本内容

c++ - 如何使 Qt 的 BorderLayout 项目可调整大小?

android - 将 DrawerLayout 与 CoordinatorLayout 和 FAB 相结合

android - 在 xml 中并排显示 DatePicker 和 TimePicker

c# - WPF 自动调整元素大小

android - 如何删除撰写 View 上的阴影?