简体   繁体   中英

Cannot set `app:layout_behavior` on fragment tag

I have a simple fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/tabLayout"
        app:layout_anchorGravity="bottom"
        android:text="test" />
</LinearLayout>

In the SampleFragment2.kt I simply inflate the above layout.

I include it below my AppBarLayout and add the app:layout_behaviour tag to it:

<com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
   <!-- ... -->
</com.google.android.material.appbar.AppBarLayout>

<fragment
        android:id="@+id/somefragment"
        android:name="com.company.sample.SampleFragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_anchor="@id/tabLayout"
        app:layout_anchorGravity="bottom" />

And this gives me runtime error:

java.lang.ClassCastException: com.google.android.material.appbar.AppBarLayout$LayoutParams cannot be cast to androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams

If I omit the fragment tag and simply put the LinearLayout from the fragment below the AppBarLayout and add the app:layout_behavior tag there, it works just fine.

How can I achieve the same behavior when using a fragment tag?

As @MikeM. pointed out in his comment, app:layout_anchor="@id/tabLayout" was the problem. I removed all anchor attributes and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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