不能将浮动操作按钮放在两个布局的中间

时间:2016-01-14 18:58:20

标签: android

我的XML文件是:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mybitchinapp.cortana.com.musica.MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:weightSum="7"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/layout_upper"
            android:layout_weight="3"
            android:layout_marginRight="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:background="#727272">
            <android.support.v7.widget.CardView

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/cardview"
                android:layout_marginRight="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/song_imgview"/>

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

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:id="@+id/layout_lower"
            android:orientation="vertical"
            android:background="#ffffff">

        </LinearLayout>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"

        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/layout_upper"
        app:layout_anchorGravity="bottom|right|end"
        android:elevation="15dp"
        android:layout_alignBottom="@id/layout_upper"
        android:layout_marginBottom="-32dp"
        android:layout_margin="16dp"
        />

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

我正在按照此问题接受的答案,将按钮置于两个布局之间:How can I add the new "Floating Action Button" between two widgets/layouts

在我的输出中,该按钮位于第一个linearLayout之下,而不是它们之间。我如何把它带到中心?

1 个答案:

答案 0 :(得分:1)

您也可以使用AppBarLayout代替这些LinearLayouts btw:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layout_upper"
        android:layout_gravity="center"
        android:layout_margin="16dp"
        android:layout_marginBottom="-32dp"
        android:elevation="15dp"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/layout_upper"
        app:layout_anchorGravity="center|bottom" />
相关问题