浮动动作按钮位置

时间:2016-06-04 11:08:08

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

我的布局底部有一个片段,当它显示时它与我的FAB重叠。
那么,我怎样才能使FAB依赖于这个Fragment(显示片段时提升FAB,就像显示Snackbar时一样)?

2 个答案:

答案 0 :(得分:0)

FABSnackbar之间的行为是将CoordinatorLayout作为两个视图的直接父级的结果。协调器布局处理此动画效果。

  

协调员布局用作特定的容器   与一个或多个子视图的交互。

     

通过为CoordinatorLayout的子视图指定行为,您可以   在单个父母和那些之间提供许多不同的互动   观点也可以互相影响。

参考:Coordinator Layout | Android Developers

所以你持有片段的视图需要有一个Behaviour实现。这将定义您的视图与其他视图的交互方式。

请检查此guide,了解如何为Behaviours创建CoordinatorLayout

对于FloatingActionButton,此行为已在其类中定义。检查来源here

答案 1 :(得分:0)

我找到了解决方案。这是我的布局:

<RelativeLayout
    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.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/mediaPlayerControlBar">

        <RelativeLayout>



        </RelativeLayout>

        <android.support.design.widget.FloatingActionButton
            app:layout_behavior="com.unimusic.ScrollAwareFABBehavior"
            app:fabSize="normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:src="@drawable/ic_search_white_24dp"
            android:id="@+id/fab"
            android:layout_margin="16dp"
            app:elevation="4dp"/>


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

    <fragment android:name="com.unimusic.mediaPlayerControlBar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:id="@+id/mediaPlayerControlBar"
        android:layout_gravity="bottom"
        app:layout_anchor="@id/snackbarPosition"
        app:layout_anchorGravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

基本上,我已将Relative布局设为根组件而非CoordinatorLayout。

相关问题