将TabLayout固定到顶部,同时使用AppBarLayout将工具栏滚动到顶部

时间:2016-03-18 00:51:03

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

我有一个TabLayout,我希望始终固定在顶部,以及下面的工具栏,当滚动视图时,向上滚动到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保持固定,而下面的工具栏在屏幕外滚动?“

如果需要,可以提供图片

1 个答案:

答案 0 :(得分:0)

根据Android Design Support Library blog post

  

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

因此AppBarLayout无法实现您的目标。