如何创建与CoordinatorLayout的内容重叠的AppBarLayout

dim*_*suz 9 android android-appcompat android-design-library android-coordinatorlayout android-support-design

在某些活动中使用CoordinatorLayoutwith时AppBarLayout,我需要将内容放在 AppBarLayout下,即工具栏使用一些透明颜色并覆盖内容.默认情况下CoordinatorLayout+ AppBarLayout排列东西,使工具栏和滚动内容彼此相邻,没有任何重叠.

Android开发人员指南在这里有关于此的文档,它看起来像这样(但这些标志似乎不适用于Toolbar和appcompat - 我试过):

覆盖ActionBar

所以我需要看起来像上面图像的东西,但所有滚动的好东西都由CoordinatorLayout+ 提供AppBarLayout.并且没有必要使用CollapsingToolbarLayout- 只是这个简单的.

有关如何实现这一目标的任何提示?这是我的活动布局.

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/top_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <include layout="@layout/main_toolbar"/>
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
        <!-- to be filled by content fragment -->
    </FrameLayout>
    <android.support.design.widget.FloatingActionButton
        style="@style/FabStyle"
        android:id="@+id/fab_button"
        android:src="@drawable/bt_filters"
        />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

GPa*_*ack 6

我试过这个解决方案,它有效。

透明度:为 AppBarLayout 添加背景,并在 AppBarLayout 之前在布局中放置滚动视图

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000" >
Run Code Online (Sandbox Code Playgroud)

内容定位:由新AppbBarTransparentScrollingViewBehavior扩展AppBarLayout.ScrollingViewBehavior重写onDependentViewChanged()和修改updateOffset(),以offset = 0

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
        View dependency) {
    updateOffset(parent, child, dependency);
    return false;
}

private boolean updateOffset(CoordinatorLayout parent, View child,
        View dependency) {
    final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency
            .getLayoutParams()).getBehavior();
    if (behavior instanceof Behavior) {
        // Offset the child so that it is below the app-bar (with any
        // overlap)
        final int offset = 0;   // CHANGED TO 0
        setTopAndBottomOffset(offset);
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

新内容的行为:在滚动视图上设置行为

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout_behavior="AppbBarTransparentScrollingViewBehavior" />
Run Code Online (Sandbox Code Playgroud)

结果:在 NestedScrollView 中使用 ImageView 作为滚动视图

在此处输入图片说明

  • 很好的答案。不要忘记在定义自定义行为时添加一个带有上下文和 attributeSet 参数的构造函数(在其中简单地调用 `super()`),否则在膨胀布局时会遇到一些问题。 (2认同)