我已经创建了一个XML,其中包含appbar布局、带有imageeview和嵌套滚动视图的折叠工具栏布局,以便预先实现视差滚动。
然而,我想在折叠工具栏中添加一个carview,并使用折叠模式将其钉住。上卷后无法钉住。我不知道我做错了什么。有谁能帮忙吗?
这是我的XML代码。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin">
//textview
</androidx.cardview.widget.CardView>
<ImageView
android:id="@+id/imageView7"
android:layout_width="360dp"
android:layout_height="360dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/logo"
tools:srcCompat="@drawable/logo" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:overScrollMode="always"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//my content
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
据我所知,只有 Toolbar
可以夹在 CollapsingToolbarLayout
. 其他一切都会折叠。所以你要么把你想钉在工具栏布局里面的布局(不推荐,会改变工具栏的大小和导航按钮的位置等),要么把它移到CollapsingToolbar外面。
只要记住 AppBarLayout
就像一个垂直的LinearLayout。
如果你的设计允许的话,有一个可能的想法是去掉 CollapsingToolbarLayout
并尝试在 AppBarLayout
. 你可以用以下方法控制哪些东西去掉,哪些东西留下。layout_scrollFlags
.