繁体   English   中英

无法在片段标签上设置 `app:layout_behavior`

[英]Cannot set `app:layout_behavior` on fragment tag

我有一个简单的片段:

<?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>

SampleFragment2.kt中,我只是简单地扩充了上述布局。

我将它包含在我的AppBarLayout下方,并将app:layout_behaviour标记添加到其中:

<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" />

这给了我运行时错误:

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

如果我省略fragment标记,只需将片段中的LinearLayout放在AppBarLayout下方并在其中添加app:layout_behavior标记,它就可以正常工作。

使用fragment标签时如何实现相同的行为?

作为@MikeM。 在他的评论中指出, app:layout_anchor="@id/tabLayout"是问题所在。 我删除了所有锚属性并且它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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