【问题标题】:How to achieve overlapping in CoordinatorLayout?CoordinatorLayout如何实现重叠?
【发布时间】:2019-01-29 08:03:10
【问题描述】:

我使用 CoordinatorLayout 来获取 Appbar + RecyclerView 滚动行为。但是我无法在此 CoordinatorLayout 中实现 Appbar 与 RecyclerView 的重叠。我尝试过使用负边距,但我不知道这是否是一个好习惯。有没有其他方法可以做到这一点?

以下代码是没有负边距的原始xml:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ...
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:elevation="0dp"
        android:clipToPadding="false"
        android:background="@color/transparent"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            ...
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

[编辑]我尝试将 Appbar + RecyclerView 封装在 RelativeLayout 中,然后再将它们放入 CoordinatorLayout(请参阅 Overlapping views in coordinatorlayout),但这只会破坏“滚动时隐藏 appbar”的行为。

【问题讨论】:

  • 请添加 XML 和代码。
  • 检查你是否添加了类似 |app:layout_behavior="@string/appbar_scrolling_view_behavior"| 的内容,尝试删除它。否则,请分享 XML 和代码。
  • @TanveerMunir 添加了 xml
  • @sagargurtu 请看我的回答

标签: android android-layout android-coordinatorlayout


【解决方案1】:

这是我正在使用这个像这样

XML:

<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="wrap_content">
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:titleTextColor="@color/white"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            android:id="@+id/toolbar">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="match_parent"
        >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycle"
        android:layout_marginTop="5dp"
        android:layout_gravity="center_horizontal|top" />
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

您正在根据您的要求使用layout_scrollFlags

请看这个URL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2023-04-03
    • 2016-07-02
    • 2018-02-17
    • 2015-12-22
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多