【问题标题】:Cannot set `app:layout_behavior` on fragment tag无法在片段标签上设置 `app:layout_behavior`
【发布时间】:2020-04-10 16:21:06
【问题描述】:

我有一个简单的片段:

<?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 标签时如何实现相同的行为?

【问题讨论】:

  • 我猜实际上是app:layout_anchor="@id/tabLayout" 导致了这个问题。 tabLayout 在你的 &lt;AppBarLayout&gt; 里面吗?
  • @MikeM。是的,tablayout 在里面。锚属性正是问题所在。我删除了锚的东西,现在它工作得很好!我永远不会想到这一点。谢谢你,你拯救了我的一天

标签: android android-fragments android-xml android-appbarlayout


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多