【问题标题】:android Place content below toolbarandroid 将内容放在工具栏下方
【发布时间】:2015-11-16 15:12:57
【问题描述】:

我有这个布局:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"/>

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

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

    </android.support.v7.widget.RecyclerView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_favorite_outline_white_24dp"/>

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

设置@string/appbar_scrolling_view_behavior 属性将RecyclerView 移动工具栏的高度。

但是如果我需要 RecyclerView 的第一个元素与状态栏对齐怎么办。
我希望工具栏覆盖(位于上方)第一个元素。

换句话说,我不想要@string/appbar_scrolling_view_behavior 行为所需要的任何偏移量。

你能告诉我怎么做吗?

【问题讨论】:

    标签: android android-support-library android-toolbar android-coordinatorlayout


    【解决方案1】:

    我遇到了同样的问题,我只是将 AppbarLayout 和我的其余视图(在你的情况下是 recyclerview)包装在一个 RelativeLayout 中,它工作正常。我不知道这种方法是否有任何缺点。

    【讨论】:

    • 我尝试换行,但 AppbarLayout 拒绝滚动
    • 啊哈,我不知道你想要那个。
    【解决方案2】:

    偏移量不是@string/appbar_scrolling_view_behavior behaviour,这是由于AppBarLayout 将内容向下推。

    我不确定是否还有其他更好的解决方案。但我建议删除AppBarLayout,让您的内容归入Toolbar。此外,您可能需要滚动行为,您可以查看下面的库。

    Jair Music Player 等许多应用都在使用它,甚至 WhatsApp 也使用它。

    图书馆: Android Observable Scroll View

    【讨论】:

    • AppBarLayout 和 Google 的支持库是必需的。无论如何,感谢您的链接,非常好的图书馆
    • @VitalyZinchenko 谢谢。如果您确实需要使用 Google 的库,请尝试使用 FrameLayout 包装 AppBarLayout。如果你愿意,我个人会使用你可以在 Jair 音乐播放器中看到的两个库的组合。
    • AppBarLayout 应该是 CoordinatorLayout 的直接子级(来自 AppBarLayout 源代码),因此包装无济于事
    • @VitalyZinchenko 哦,是的,我忘了。你能更详细地说明你的用例吗?也许你可以像我一样拥有这两个库的组合
    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多