2016-12-04 92 views
0

由于某种原因,我的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预览总是在右下角显示晶圆厂按钮,但是当我在手机上运行它时,它在右上角。

编辑:这里是我的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> 
+0

只需添加机器人:layout_gravity =“底部|结束”,删除所有其他重力相关的标记 – Swapnil

+0

尝试添加额外的FrameLayout请参阅我的答案,如果有帮助 – Swapnil

回答

4

问题是由于android.support.v4.widget.NestedScrollView增加一个字段android:fillViewport="true". Issue was coming as your NestedScrollView`没有展开以匹配父母或获取可用空间。我的代码工作正常,测试

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

不是。仍然没有工作。 – Jared

+0

请检查以上编辑的答案,其工作正常我 – Swapnil

+0

是的!简单的修复。谢谢! – Jared

3

FloatingActionButton删除线

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

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

+0

我做到了,它仍然无法正常工作。顺便说一句,XML预览总是显示在右下角的晶圆厂按钮,但是当我在手机上运行它时,它在右上角。 – Jared

+0

你的'MyPollsFragment'你实际使用这个布局填充屏幕吗?或者它是否具有'wrap_content'作为它放置在布局中的高度? – ianhanniballake

+0

是的,它填满了屏幕。我将添加我的主要活动xml以显示我放置片段的位置。 – Jared

2

从浮动操作按钮的XML代码删除android:layout_gravity="bottom|end" ...它会工作