【问题标题】:Pin TabLayout to top while Toolbar below scrolls into it using AppBarLayout将 TabLayout 固定到顶部,而下方的 Toolbar 使用 AppBarLayout 滚动到其中
【发布时间】:2016-07-04 14:41:32
【问题描述】:

我有一个我希望始终固定在顶部的 TabLayout,还有一个下方的 Toolbar,当视图滚动时,它会向上滚动到 TabLayout。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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"
    tools:context=".main.MainActivity">



    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:animateLayoutChanges="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>


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

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

    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView2"
            android:src="@drawable/music_content"
            android:contentDescription="@string/image_chooser_title"
            />

    </android.support.v4.widget.NestedScrollView>

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

你可以在这里看到我已经设置了工具栏

app:layout_scrollFlags="scroll|enterAlways"

并且不为上面的 TabLayout 设置任何滚动标志,因为我希望它保持固定。但是,使用这些设置,AppBarLayout 根本不会滚动。如果我向 TabLayout 添加滚动标志,那么它们都会滚动,并且 TabLayout 不会保持固定。有没有办法让 TabLayout 保持固定,而下面的工具栏向上滚动“屏幕外”?

如有需要可提供图片

【问题讨论】:

    标签: android android-support-library android-toolbar android-design-library android-appbarlayout


    【解决方案1】:

    根据Android Design Support Library blog post

    注意:所有使用滚动标志的视图必须在不使用该标志的视图之前声明。这样可以确保所有视图都从顶部退出,而将固定元素留在后面。

    所以AppBarLayout 无法实现您想要的。

    【讨论】:

    猜你喜欢
    • 2020-07-02
    • 2016-08-18
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2019-12-10
    相关资源
    最近更新 更多