android - 无法在片段标签上设置 `app:layout_behavior`
问题描述
我有一个简单的片段:
<?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"
是问题所在。我删除了所有锚属性并且它起作用了。
推荐阅读
- c - 如何静态构建使用 CGO 的 Go 文件?
- java - JDK 1.8.x 的要求检查失败!检测到的版本:3.0.0
- reactjs - 如果使用带有元素样式 Reactjs 的 useRef() 条件,UI 不会更新
- azure - 为什么此 Azure 资源管理器模板验证失败?
- kubernetes - Kubernetes 生命周期命令未运行
- node.js - 如何在 MongoDB 中管理连接
- r - 匹配相同的列
- python - 从 Pandas Dataframe 创建 Networkx 有向图时如何创建多线节点标签
- python - 如何使用moviepy反转视频和音频剪辑?
- sql - 如何在Postgres中按年月按最大(日期)组获取行?