【问题标题】:Floating Action Button Not In Bottom Right浮动操作按钮不在右下角
【发布时间】:2016-12-04 01:16:10
【问题描述】:

由于某种原因,我的 FAB 不会像往常一样转到屏幕的右下角。我在一个片段中,那个片段是一个协调器布局(我不知道这是否是一个好习惯)。

这是布局xml代码:

<android.support.design.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="com.gigstudios.polls.fragment.MyPollsFragment">

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_36dp"
        app:fabSize="normal"
        app:layout_anchor="@id/linear_layout"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

由于某种原因,FAB 最终位于右上角而不是右下角。有谁知道我做错了什么?

顺便说一句,xml 预览总是在右下角显示 fab 按钮,但是当我在手机上运行它时,它却在右上角。

编辑:这是我的 main_activity xml,用于显示我放置片段的位置(“容器”FrameLayout)。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGrey">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorGrey"
        android:layout_marginTop="?attr/actionBarSize">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/container">
        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 只需添加 android:layout_gravity="bottom|end",删除所有其他重力相关标签
  • 尝试添加额外的 FrameLayout 看看我的回答是否有帮助

标签: android android-coordinatorlayout gravity floating-action-button


【解决方案1】:

问题是由于android.support.v4.widget.NestedScrollView 再添加一个字段android:fillViewport="true". Issue was coming as yourNestedScrollView` 没有扩展以匹配父级或占用可用空间。用我的代码测试工作正常

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:fillViewport="true"
    android:layout_marginTop="?attr/actionBarSize">
</android.support.v4.widget.NestedScrollView>

【讨论】:

  • 请检查上面编辑的答案,它对我来说工作正常
【解决方案2】:

删除线

app:layout_anchor="@id/linear_layout"
app:layout_anchorGravity="bottom|right|end"

在您的FloatingActionButton 上。他们没有做任何事情(因为您正在锚定到填充整个CoordinatorLayout 的视图)。这将使您的layout_gravity 受到尊重,并且您的FloatingActionButton 将被放置在CoordinatorLayout 的右下角。

【讨论】:

  • 我这样做了,但它仍然不起作用。顺便说一句,xml 预览总是在右下角显示 fab 按钮,但是当我在手机上运行它时,它却在右上角。
  • 您的MyPollsFragment 是否使用此布局实际上填满了屏幕?或者它在您放置它的布局中是否具有wrap_content 的高度?
  • 是的,它填满了屏幕。我将添加我的主要活动 xml 以显示我放置片段的位置。
【解决方案3】:

从您的浮动操作按钮xml 代码中删除android:layout_gravity="bottom|end"...它将起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2020-11-15
    • 2019-11-28
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多