TabLayout上方的标题布局

时间:2015-09-12 22:59:56

标签: android android-layout android-fragments android-activity android-xml

我试图做这样的事情:

http://i.imgur.com/NwewaBx.png

我目前没有实现标头的上述布局。我只需要弄清楚如何添加标题。

这是我想要放在标签上方的标题布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:background="@color/green">

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

                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@drawable/ic_image"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:layout_gravity="center_vertical" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hey!"
                    android:textColor="@color/white"
                    android:textSize="24sp"
                    android:layout_gravity="center_vertical" />

            </LinearLayout>

    </LinearLayout>

</LinearLayout>

我在标签下方的内容中使用了RecyclerView。我为Fragments使用了3个不同的Activity

这是我的活动:

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:background="@color/blue"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/TabLayout"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/white"
            />

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

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

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

这是我的一个碎片:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

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

如何在TabLayout上方添加标题并保持正常的滚动行为?

1 个答案:

答案 0 :(得分:0)

你只需要一个RecyclerView来做到这一点。 将标题和tabLayout组合到标题视图,然后添加到RecyclerView。

检查https://github.com/u3breeze/android-RecyclerView-WithHeaderAndFooter。我做了RecyclerView.Adapter的扩展。易于向RecyclerView添加页眉和页脚。