来自设计库的FAB - 错误的位置

时间:2015-09-28 21:56:10

标签: android android-design-library floating-action-button

最近我一直试图使用谷歌的新设计库,但FAB View没有像我预期的那样工作。

这是我的布局的样子(我在CoordinatorLayout中删除了那些布局的子项以使其更清晰)

<?xml version="1.0" encoding="utf-8"?>
<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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/action_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:selectableItemBackground"
        android:minHeight="@dimen/material_54"/>

    <RelativeLayout
        android:id="@+id/action_rewrite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:selectableItemBackground"
        android:minHeight="@dimen/material_54"/>

    <include layout="@layout/shadow_down" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/window_background"/>

</LinearLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/action_import"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:src="@drawable/log_cell_changed"
    android:layout_marginRight="@dimen/space_16"
    android:layout_marginBottom="@dimen/space_16"
    app:borderWidth="0dp"
    app:elevation="4dp"
    app:fabSize="normal" />

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

问题是我运行应用程序时屏幕上没有FAB。但Android Studio上的“预览”会正确显示。 当我添加

时,我尝试使用它

android:layout_marginBottom="110dp"

到FAB会导致FAB出现在应该放置的位置而没有额外的余量。

Result of preview in Android Studio Real result when I run the app

1 个答案:

答案 0 :(得分:0)

使用layout_alignParentBottom和layout_alignParentRight为true,如下例所示

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="@dimen/fac_margin_bottom"
            android:layout_marginRight="@dimen/fab_margin_right"
            android:src="@mipmap/fab_add"
            app:backgroundTint="@color/fab_color"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:fabSize="normal"
            app:rippleColor="@android:color/white" />