与 AppBarLayout 重叠滚动视图 [英] Overlap scrolling view with AppBarLayout
问题描述
我想从 来做到这一点?
事实上,用 AppBarLayout 覆盖滚动视图是 Android 设计支持库:您可以在 NestedScrollView 上使用
(或任何使用 ScrollingViewBehavior) 设置重叠量:app:behavior_overlapTop
属性
请注意,app:behavior_overlapTop
仅适用于具有 app:layout_behavior="@string/appbar_scrolling_view_behavior"
的视图,因为它是使用属性 (不是视图或父视图组,因为属性通常适用于),因此是 behavior_
前缀.
NestedScrollView scrollView = ...CoordinatorLayout.LayoutParams 参数 =(CoordinatorLayout.LayoutParams) scrollView.getLayoutParams();AppBarLayout.ScrollingViewBehavior 行为 =(AppBarLayout.ScrollingViewBehavior) params.getBehavior();行为.setOverlayTop(128);//注意:以像素为单位
I want to implement the 'Flexible Space with overlapping content' pattern from the Material design scrolling techniques, such as in this video:
My XML layout right now looks like:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="192dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<....>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Is there an easy way to accomplish this using the Design Library? Or do I have to build a custom CoordinatorLayout.Behavior to do this?
In fact, overlaying the scrolling view with the AppBarLayout is an included feature of the Android Design Support Library: you can use the app:behavior_overlapTop
attribute on your NestedScrollView
(or any View using ScrollingViewBehavior) to set the overlap amount:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:behavior_overlapTop="64dp">
Note that app:behavior_overlapTop
only works on views that have the app:layout_behavior="@string/appbar_scrolling_view_behavior"
as it is the Behavior that is using the attribute (not the View or the Parent ViewGroup, as attributes usually apply to), hence the behavior_
prefix.
Or set it programmatically via setOverlayTop():
NestedScrollView scrollView = ...
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) scrollView.getLayoutParams();
AppBarLayout.ScrollingViewBehavior behavior =
(AppBarLayout.ScrollingViewBehavior) params.getBehavior();
behavior.setOverlayTop(128); // Note: in pixels
这篇关于与 AppBarLayout 重叠滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!