Android 将TabLayout固定到工具栏滚动视图的顶部和下方

Android 将TabLayout固定到工具栏滚动视图的顶部和下方,android,xml,android-studio,android-layout,Android,Xml,Android Studio,Android Layout,我想在滚动时锁定TabLayout 我试过……密码。但无法在工具栏下方固定TabLayout 在这个我有工具栏,下面我有scrollview,在scrollview中我有很多布局 我想锁定ViewPager上方(底部)的TableLayout。当我向下滚动时,tabLayout被固定,并且显示视图寻呼机(看起来有点像tabLayout被固定在工具栏下方) 以下是示例图像: 示例视频: 活动\u main.xml <?xml version="1.0" encoding="utf-8"?

我想在滚动时锁定TabLayout

我试过……密码。但无法在工具栏下方固定TabLayout

在这个我有工具栏,下面我有scrollview,在scrollview中我有很多布局

我想锁定ViewPager上方(底部)的TableLayout。当我向下滚动时,tabLayout被固定,并且显示视图寻呼机(看起来有点像tabLayout被固定在工具栏下方)

以下是示例图像:

示例视频:

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="#EAEAEA"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#03A9F4"
        android:elevation="3dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:padding="4dp"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:elevation="2dp"
                app:cardCornerRadius="20dp"
                app:cardUseCompatPadding="true" />

            <androidx.cardview.widget.CardView
                android:id="@+id/live_wallpaper_card"
                android:layout_width="match_parent"
                android:layout_height="120dp"
                android:elevation="2dp"
                app:cardCornerRadius="20dp"
                app:cardUseCompatPadding="true" />


            <!--Todo: horizontal card scroll-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:text="Category"
                        android:textColor="#000"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/more_textTv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:text="More"
                        android:textStyle="bold" />


                </RelativeLayout>

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="none">


                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <androidx.cardview.widget.CardView
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            app:cardCornerRadius="15dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            app:cardCornerRadius="15dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            app:cardCornerRadius="15dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            app:cardCornerRadius="15dp"
                            app:cardUseCompatPadding="true" />


                    </LinearLayout>


                </HorizontalScrollView>


            </LinearLayout>

            <!--Todo: Featured Album horizontal scroll view-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Featured Albums"
                        android:textColor="#000"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                </RelativeLayout>


                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <androidx.cardview.widget.CardView
                            android:layout_width="300dp"
                            android:layout_height="220dp"
                            app:cardCornerRadius="20dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="300dp"
                            android:layout_height="220dp"
                            app:cardCornerRadius="20dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="300dp"
                            android:layout_height="220dp"
                            app:cardCornerRadius="20dp"
                            app:cardUseCompatPadding="true" />

                        <androidx.cardview.widget.CardView
                            android:layout_width="300dp"
                            android:layout_height="220dp"
                            app:cardCornerRadius="20dp"
                            app:cardUseCompatPadding="true" />


                    </LinearLayout>

                </HorizontalScrollView>

            </LinearLayout>


            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/holo_blue_bright"
                app:tabIndicatorColor="@color/colorPrimary"
                app:tabIndicatorHeight="3dp"
                app:tabSelectedTextColor="@color/black"
                app:tabTextColor="@android:color/darker_gray" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="650dp"
                    android:background="@color/colorPrimaryDark" />


            </LinearLayout>

            <!--
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical">



            </LinearLayout>
            -->

        </LinearLayout>


    </ScrollView>

</LinearLayout>



答案如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">


        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FAFAFA"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="#03A9F4"
                app:expandedTitleTextAppearance="@android:color/transparent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="100dp"
                    android:orientation="vertical"
                    app:layout_collapseMode="parallax">


                    <androidx.cardview.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="180dp"
                        android:elevation="2dp"
                        app:cardCornerRadius="20dp"
                        app:cardUseCompatPadding="true" />

                    <androidx.cardview.widget.CardView
                        android:id="@+id/live_wallpaper_card"
                        android:layout_width="match_parent"
                        android:layout_height="120dp"
                        android:elevation="2dp"
                        app:cardCornerRadius="20dp"
                        app:cardUseCompatPadding="true" />


                    <!--Todo: horizontal card scroll-->
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:padding="8dp">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:text="Category"
                                android:textColor="#000"
                                android:textSize="16sp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/more_textTv"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentEnd="true"
                                android:text="More"
                                android:textColor="#BF000000"
                                android:textStyle="bold" />


                        </RelativeLayout>

                        <HorizontalScrollView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:scrollbars="none">


                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <androidx.cardview.widget.CardView
                                    android:layout_width="100dp"
                                    android:layout_height="100dp"
                                    app:cardCornerRadius="15dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="100dp"
                                    android:layout_height="100dp"
                                    app:cardCornerRadius="15dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="100dp"
                                    android:layout_height="100dp"
                                    app:cardCornerRadius="15dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="100dp"
                                    android:layout_height="100dp"
                                    app:cardCornerRadius="15dp"
                                    app:cardUseCompatPadding="true" />


                            </LinearLayout>


                        </HorizontalScrollView>


                    </LinearLayout>

                    <!--Todo: Featured Album horizontal scroll view-->
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">


                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Featured Albums"
                                android:textColor="#000"
                                android:textSize="16sp"
                                android:textStyle="bold" />

                        </RelativeLayout>


                        <HorizontalScrollView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:scrollbars="none">

                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">

                                <androidx.cardview.widget.CardView
                                    android:layout_width="300dp"
                                    android:layout_height="220dp"
                                    app:cardCornerRadius="20dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="300dp"
                                    android:layout_height="220dp"
                                    app:cardCornerRadius="20dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="300dp"
                                    android:layout_height="220dp"
                                    app:cardCornerRadius="20dp"
                                    app:cardUseCompatPadding="true" />

                                <androidx.cardview.widget.CardView
                                    android:layout_width="300dp"
                                    android:layout_height="220dp"
                                    app:cardCornerRadius="20dp"
                                    app:cardUseCompatPadding="true" />


                            </LinearLayout>

                        </HorizontalScrollView>

                    </LinearLayout>


                </LinearLayout>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbarMain"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark" />


            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <com.google.android.material.tabs.TabLayout
                        android:id="@+id/tabLayout"
                        app:tabIndicatorFullWidth="false"
                        app:tabIndicatorColor="#FFEB3B"
                        app:tabSelectedTextColor="#FFEB3B"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:tabTextColor="@color/colorPrimaryDark" />

                    <androidx.viewpager.widget.ViewPager
                        android:id="@+id/pager"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



                </LinearLayout>


            </androidx.core.widget.NestedScrollView>

        </LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>