FloatingActionButton锚点位于视图

时间:2016-05-23 07:27:14

标签: android floating-action-button

我想将FloatingActionButton锚定在视图的顶部,
这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.activity.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <com.alirezaafkar.toolbar.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:direction="rtl"
                app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@string/app_name">

                <TextView
                    android:id="@+id/tv_toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />

            </com.alirezaafkar.toolbar.Toolbar>

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

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

            <FrameLayout
                android:id="@+id/container_body"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </FrameLayout>

        <ViewSwitcher
            android:id="@+id/viewSwitcherBottomBar"
            android:layout_width="wrap_content"
            android:layout_height="120dp"
            android:layout_gravity="bottom"
            android:inAnimation="@android:anim/slide_in_left">

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

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


        </ViewSwitcher>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_my_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            android:layout_marginBottom="60dp"
            android:paddingBottom="60dp"
            android:src="@drawable/ic_my_location_white_24dp"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:fabSize="mini"
            app:layout_anchor="@id/viewSwitcherBottomBar"
            app:layout_anchorGravity="top|right" />


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


这就是结果,
但结果并不完全在视图的顶部:

enter image description here


我怎样才能将floatingActionButton精确地放在视图的顶部?
编辑
我希望floatingActionButton不与锚定视图重叠。

2 个答案:

答案 0 :(得分:2)

而不是给FloatingActionButton赋予填充和边距,我在视图中设置填充并且它有效:

    <ViewSwitcher
        android:id="@+id/viewSwitcherBottomBar"
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:paddingTop="25dp"
        android:layout_gravity="bottom"
        android:inAnimation="@android:anim/slide_in_left">

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

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

    </ViewSwitcher>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_my_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/ic_my_location_white_24dp"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="mini"
    app:layout_anchor="@id/viewSwitcherBottomBar"
    app:layout_anchorGravity="top|right" />

答案 1 :(得分:0)

以下三行必不可少:

android:layout_gravity="top"
app:layout_anchor="@+id/layout_store_detail"
app:layout_anchorGravity="top|end"

整个XML看起来像这样:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- This is the bottom sheet which pops up -->
    <LinearLayout
        android:id="@+id/layout_store_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:behavior_skipCollapsed="true"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        >
        ...
    </LinearLayout>


    <!-- This is the Floating action button with a margin of 16dp -->
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:layout_anchor="@+id/layout_store_detail"
        app:layout_anchorGravity="top|end"
        >
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_locate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:src="@drawable/globe"
            />
    </FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>