Android-如何使用TabLayout从屏幕中间开始垂直滚动整个内容?

时间:2018-08-06 07:04:08

标签: android scroll android-tablayout

我有3个选项卡的TabLayout,并且效果很好。如果内容不适合屏幕,则选项卡在内部滚动。现在,我想垂直滚动除工具栏以外的全部内容。当前,我正在内部滚动TabLayout,但是我需要滚动从屏幕中间开始的页眉LinearLayout和TabLayout。

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".GameInfoActivity">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#ffffff">

                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:visibility="gone" />

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

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:orientation="horizontal"
                        android:paddingBottom="8dp"
                        android:paddingTop="8dp">

                        <TextView
                            android:id="@+id/team1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:maxWidth="240dp"
                            android:text="Team 1"
                            android:textColor="@color/colorBlackBG"
                            android:textSize="15sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text=" - "
                            android:textColor="@color/colorBlackBG" />

                        <TextView
                            android:id="@+id/team2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:maxWidth="240dp"
                            android:text="Team 2"
                            android:textColor="@color/colorBlackBG"
                            android:textSize="15sp" />

                    </LinearLayout>

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

                        <TextView
                            android:id="@+id/gameInfo_league_name"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="center_horizontal"
                            android:text="League Name" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        android:orientation="horizontal"
                        android:weightSum="3">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/gameInfo_flag1"
                                android:layout_width="70dp"
                                android:layout_height="70dp"
                                android:layout_marginTop="8dp"
                                android:text="date"
                                android:textColor="#b71c1c"
                                android:textSize="14sp"
                                app:srcCompat="@drawable/game_info_flag_placeholder" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:orientation="vertical">

                            <TextView
                                android:id="@+id/score"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="0:0"
                                android:textColor="#777777"
                                android:textSize="24sp" />

                            <TextView
                                android:id="@+id/date"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="8dp"
                                android:text="date"
                                android:textColor="#b71c1c"
                                android:textSize="14sp" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/gameInfo_flag2"
                                android:layout_width="70dp"
                                android:layout_height="70dp"
                                android:layout_marginTop="8dp"
                                android:text="date"
                                android:textColor="#b71c1c"
                                android:textSize="14sp"
                                app:srcCompat="@drawable/game_info_flag_placeholder" />

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

            </RelativeLayout>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimaryDark"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabIndicatorHeight="4dp"
                app:tabSelectedTextColor="@android:color/white"
                app:tabTextColor="@android:color/white">

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/match_details" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/head_to_head" />

                <android.support.design.widget.TabItem
                    android:id="@+id/tabItem3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/prediction" />

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

            <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </LinearLayout>

</LinearLayout>

屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

通常,您使用ScrollView。

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

        <!-- Content starts here -->

    </LinearLayout>
</ScrollView>

所有应该像一个大元素一样“滚动”的元素应该在 ScrollView

内的 LinearLayout