繁体   English   中英

如何使用CoordinatorLayout正确固定“浮动操作按钮”?

[英]How to pin “Floating Action Button” correcly using CoordinatorLayout?

我使用CoordinatorLayout作为我的根视图。 它包含一个RelativeLayout和一个FloatingActionButton 当我为FloatingActionButton设置layout_anchorlayout_anchorGravity ,它没有像我预期的那样居中于RelativeLayout的边缘(屏幕上的橙色区域)。

我尝试过解决这个问题的方法, 如何在两个小部件/布局之间添加新的“浮动操作按钮”,但没有成功。

这是我的布局:

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

    <RelativeLayout
        android:id="@+id/primary_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:paddingBottom="30dp">

        <com.vocabularyminer.android.android.view.view.FloatingEditText
            android:id="@+id/edittext_package_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_marginLeft="14dp"
            android:layout_marginRight="14dp"
            android:layout_marginTop="16dp"
            android:hint="Test fab button"/>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_done_white_24dp"
        app:elevation="4dp"
        app:layout_anchor="@id/primary_area"
        app:layout_anchorGravity="bottom|right|end"/>

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

结果屏幕: 根据上面的布局文件的结果屏幕

我所期望的:

在此处输入图片说明

我习惯于通过此代码来锚定自己,以添加到主视图的addOnLayoutChangeListener侦听器中:

// fabGap is the right margin from the CoordinatorLayout
// container is your CoordinatorLayout  

final FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP);
    layoutParams.setMargins(mAppBarLayout.getRight() - (2 * fabSize) - (2 * fabGap), mAppBarLayout.getBottom() - (fabSize / 2), 0, fabPadding);

    mFavoriteActionButton = new FloatingActionButton(getActivity());

我发现了我的问题所在。 解决此问题的方法: 如何在两个小部件/布局之间添加新的“浮动操作按钮”正确的 但是我将片段放到了根据以下xml定义的FrameLayout中:

<FrameLayout
        android:id="@+id/floating_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

而且因为我使用wrap_content,所以FAB按钮被放置在RelativeLayout内部,而不是RelativeLayout边缘的位置。

当我还在layout_heigh参数match_parent中使用时,一切都会按预期进行。

暂无
暂无

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

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