视图无法锚定到父 CoordinatorLayout

View can not be anchored to the the parent CoordinatorLayout

这不是我的应用程序,这是将源代码放在 github 上的北欧应用程序,我正在尝试 运行 我机器上的源代码。但我一直有这个错误:

03-07 07:12:13.641  12622-12622/com.noxel.apppaneladmintry2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.noxel.apppaneladmintry2, PID: 12622
java.lang.IllegalStateException: View can not be anchored to the the parent CoordinatorLayout
        at android.support.design.widget.CoordinatorLayout$LayoutParams.resolveAnchorView(CoordinatorLayout.java:2522)
        at android.support.design.widget.CoordinatorLayout$LayoutParams.findAnchorView(CoordinatorLayout.java:2491)
        at android.support.design.widget.CoordinatorLayout.prepareChildren(CoordinatorLayout.java:619)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:670)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
        at android.view.View.measure(View.java:17442)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:921)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:716)

这是我的 Mainactivity 的布局:

    <?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        style="@style/HeaderBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            app:titleTextAppearance="@style/ActionBar.TitleText"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@color/tabSelectedTextColor"
            app:tabTextColor="@color/tabTextColor"
            app:tabIndicatorColor="@color/tabIndicatorColor"
            app:tabGravity="fill"
            app:tabMode="fixed"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/fab_ic_add"
        app:backgroundTint="@color/fab_normal"
        app:layout_anchor="@+id/container"
        app:layout_anchorGravity="bottom|right|end"
        app:elevation="4dp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/background_title"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="6dp"/>

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

这是我的垃圾邮件屏幕代码:

public class SplashscreenActivity extends Activity {
    /** Splash screen duration time in milliseconds */
    private static final int DELAY = 1000;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);

        // Jump to MainActivity after DELAY milliseconds
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final Intent intent = new Intent(SplashscreenActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.putExtra(MainActivity.OPENED_FROM_LAUNCHER, true);
                startActivity(intent);
                finish();
            }
        }, DELAY);
    }

    @Override
    public void onBackPressed() {
        // do nothing. Protect from exiting the application when splash screen is shown
    }

}

这是我的闪屏布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashscreenActivity" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="70dp"
    android:scaleType="center"
    android:src="@drawable/nordic_logo" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/background_title"
    android:layout_gravity="bottom|center"
    android:layout_marginBottom="6dp"/>

谁能告诉我如何解决这个问题?

这开始发生在支持设计库 v.23.2.0

compile 'com.android.support:design:23.2.0'

我知道这是从 v23.2.0 开始的,因为我的应用程序之前运行良好 (v.23.1.2),但在更新后开始出现同样的故障。

错误很简单:

您的一个视图已锚定到其父 Coordinator Layout。但是,从 v.23.2.0 开始,不再允许这样做。

我认为错误出在您的 FAB 中:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_add"
    ...
    app:layout_anchor="@+id/container" 
    ... />

我相信 id/container 是父 CoordinatorLayout

修复中

正确的方法是更改​​ layout_anchor 并使用另一个视图作为锚点(父布局或固定在屏幕底部的任何其他视图)。

更改app:layout_anchor,不使用协调器

锚定 FAB 的视图必须是 CoordinatorLayout 的后代视图。

并且如果view是layout,FAB一定不能在layout里面,否则锚点重力设置将不起作用。例如:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/content_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    .
    .
    .
    <FrameLayout
        android:id="@+id/attachment_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/fab_margin_end"
        android:layout_marginTop="@dimen/fab_margin_top"
        android:scaleType="center"
        android:src="@drawable/ic_action_save"
        app:borderWidth="0dp"
        app:elevation="@dimen/fab_elevation"
        app:layout_anchor="@id/attachment_layout"
        app:layout_anchorGravity="top|end"
        app:rippleColor="@color/accent_highlight" />

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

我朋友今天学到了另一种方法,就是使用 Studio 中的“设计”选项卡。我拖着我的 FAB 锚定并找出这段代码 --

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_add"
        android:layout_gravity="center_vertical|center_horizontal"
        app:layout_anchor="@+id/appBar"
        app:layout_anchorGravity="bottom|right" />

请注意,这在我使用折叠工具栏时效果很好。我什至尝试设置不同的宽度并且效果很好。

Also it is working in any version of support library . Mine is 25.0.0

这是一个老问题,但我有最好的解决方案。

使用android:layout_gravity="bottom|end"(任何适合你的值) 注意 android 命名空间

此属性根据父视图布局定位 FAB。无需创建虚拟 FrameLayout 来锚定。

所有其他人都非常困惑试试这个

  <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_add_notes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_add"
            app:fabSize="normal"
            app:layout_anchor="@id/contentFrame"
            app:layout_anchorGravity="bottom|right|end"/>
    </android.support.design.widget.CoordinatorLayout>