android 实现TabLayout吸顶,title悬浮效果

之前项目需要做这样的一个效果,小费周折实现后,设计居然不要了,很是心塞。

记录下来,万一设计大大又变卦了呢(心中十万个友好问候)?

下面是我的UI界面,丑了一点,凑合看看

android 实现TabLayout吸顶,title悬浮效果

一个activity 里面嵌套两个fragment,布局文件:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <include layout="@layout/tilte"></include>
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="com.yihui.wealthe.widgets.FlingBehavior"
            android:fitsSystemWindows="true">
            <LinearLayout
                app:layout_scrollFlags="scroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                

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

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

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="@color/white"
                android:padding="5dp"
                app:tabIndicatorColor="@color/red"
                app:tabIndicatorHeight="1dp"
                app:tabSelectedTextColor="@color/red"
                app:tabTextColor="@color/black_87" />

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v4.view.ViewPager>
        </LinearLayout>

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

</LinearLayout>

利用coordinatorlayout +appbarlayout+tablayout 实现具体效果,

coordinatorlayout 里面嵌套的布局,添加

app:layout_scrollFlags="scroll"
的会滑动,然后用
app:layout_behavior=

来调控,很简单的。

这几个控件的使用 有很多资源,大家可以自行谷歌。