简体   繁体   English

浮动操作按钮不在右下角

[英]Floating Action Button Not In Bottom Right

My FAB for some reason won't go to the bottom right corner of the screen as usual.由于某种原因,我的 FAB 不会像往常一样进入屏幕的右下角。 I'm inside a fragment and that fragment is a coordinator layout (I don't know if this is a good practice or not).我在一个片段中,该片段是一个协调器布局(我不知道这是否是一个好习惯)。

Here is the layout xml code:这是布局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>

And for some reason the FAB ends up in the top right instead of the bottom right.出于某种原因,FAB 最终位于右上角而不是右下角。 Does anyone know what I'm doing wrong?有谁知道我做错了什么?

Btw the xml preview has always shown the fab button in the bottom right corner but when I run it on my phone it's in the top right instead.顺便说一句,xml 预览总是在右下角显示 fab 按钮,但是当我在手机上运行它时,它在右上角。

Edit: Here's my main_activity xml to show where I'm putting the fragment (The "container" FrameLayout).编辑:这是我的 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>

The issue is coming due to android.support.v4.widget.NestedScrollView add one more field android:fillViewport="true". Issue was coming as your这个问题是由于android.support.v4.widget.NestedScrollView添加了一个字段android:fillViewport="true". Issue was coming as your android:fillViewport="true". Issue was coming as your NestedScrollView` was not expanding to match parent or take available space. android:fillViewport="true". Issue was coming as your NestedScrollView 未扩展以匹配父级或占用可用空间,因此出现问题。 Working fine tested with my code用我的代码测试工作正常

<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>

Remove the lines删除线条

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

on your FloatingActionButton .在您的FloatingActionButton They aren't doing anything (since you are anchoring to a view that fills the entire CoordinatorLayout ).他们没有做任何事情(因为您锚定到填充整个CoordinatorLayout的视图)。 This will cause your layout_gravity to be respected and your FloatingActionButton will be placed in the bottom right corner of the CoordinatorLayout .这将导致您的layout_gravity受到尊重,并且您的FloatingActionButton将被放置在CoordinatorLayout右下角。

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

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

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