按钮位于其下方定义的布局下

Button goes under a layout which is defined below it

您好,我的布局 "addfab" 中有一个很棒的按钮,它在大多数手机中都能正确显示,但在 android 中,4.4 kitkat 隐藏在 [=18= 中定义的相对布局下] 两者都被定义为低于一个对象 这是我的 xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fbutton="http://schemas.android.com/tools"
android:weightSum="1">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bar1"

    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:textAlignment="center"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        >

    </android.support.v7.widget.Toolbar>



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


<android.support.design.widget.FloatingActionButton
    android:id="@+id/addfab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@id/bar1"
    android:layout_marginRight="20dp"
    android:layout_marginTop="130dp"
    android:clickable="true"
    android:elevation="2dp"
    android:src="@drawable/logo_green"
    app:fabSize="normal"
    />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/pic"
    android:layout_below="@id/bar1"
    android:background="@color/colorPrimary">
    <com.mikhaellopez.circularimageview.CircularImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/image_gol"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        app:civ_border_color="@color/colorPrimary"
        app:civ_border_width="5dp"
        app:civ_shadow="false"
        app:civ_shadow_radius="5"
        app:civ_shadow_color="#9e9e9e"/>

</RelativeLayout>

我该怎么办?为什么 android 的行为不同?

代替RelativeLayout,使用CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
//Toolbar here
</android.support.design.widget.AppBarLayout>

// your fab and relativelayout here
</android.support.design.widget.CoordinatorLayout>

作为根级标签。您可以使用以下方法将它(FloatingActionButton)锚定到您想要的视图:

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

属性。这还允许您在将来拥有更多功能,例如隐藏 Toolbar 等。为了使用 CoordinatorLayout.

,您还需要稍后更改并删除旧 RelativeLayout 的属性

看看: https://guides.codepath.com/android/floating-action-buttons