【发布时间】:2025-12-02 20:40:01
【问题描述】:
我希望自定义视图在滚动期间折叠并带有过渡。
我有AppBarLayout,里面有一个Toolbar。下面有一个我想折叠的自定义视图。
自定义视图下方有一个NestedScrollView 和LinearLayout。
工具栏为绿色,自定义布局为粉色,线性滚动为灰色:
向下滚动后:
<android.support.design.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"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginTop="?attr/actionBarSize"
android:background="@drawable/background"
android:gravity="center">
</RelativeLayout>
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="170dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
layout="@layout/linear"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
我应该使用自定义行为和CoordinatorLayout 还是使用带有翻译动画的NestedScroll 进行翻译?
【问题讨论】:
-
对我来说,我会将您的自定义视图包装到
CollapsingToolbarLayout中并使用自定义行为。或者,您可能还想使用OnOffsetChangedListener或AppBarLayout进行此转换。 -
@rom4ek 是否可以在
CollapsingToolbarLayout的自定义位置放置两个图像视图? -
您可以将它们保存在您现在拥有的
RelativeLayout中。只需将这个RelativeLayout放在CollapsingToolbarLayout中。 -
@rom4ek 但上部工具栏(带抽屉的)保持不变,
CollapsingToolbarLayout用于在AppBarLayout内部使用 -
是的,你是对的。但是您不需要将工具栏保留在
AppBarLayout中。你可以把它从AppBarLayout中拉出来,然后放在你的xml 中AppBarLayout之后。所以你的AppBarLayout将只包含一个CollapsingToolbarLayout,里面有你的自定义视图。检查我的回答*.com/a/44647978/3225458,这不是你想要实现的,但你至少可以从中理解xml结构。
标签: android android-coordinatorlayout android-collapsingtoolbarlayout coordinator-layout nestedlayout