无法找到coordinatorlayout的锚点

时间:2016-01-14 16:45:04

标签: android

这是我的布局:

<?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_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@+id/layout_upper"
        android:layout_weight="3">
        <android.support.v7.widget.CardView

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

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                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">

    </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:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        app:layout_anchor="@id/layout_upper"
        app:layout_anchorGravity="bottom|right|end"
        />
</android.support.design.widget.CoordinatorLayout>

我需要将FloatingActionButton放在两个布局的中心。但是在使用此布局运行应用程序时,堆栈跟踪是:

      java.lang.IllegalStateException: Could not find CoordinatorLayout descendant view with id mybitchinapp.cortana.com.musica:id/layout_upper to anchor view android.support.design.widget.FloatingActionButton{b0a6a5b VFED..C.. ......I. 0,0-0,0 #7f0c0070 app:id/fab}
                                                                                 at android.support.design.widget.CoordinatorLayout$LayoutParams.resolveAnchorView(CoordinatorLayout.java:2489)

为什么它找不到锚布局?它已在此布局文件中定义。请帮忙

1 个答案:

答案 0 :(得分:2)

 <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@+id/layout_upper"
        android:layout_weight="3">

 <LinearLayout
        android:id="@+id/layout_upper"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3">