Android Support Design庫之AppBarLayout

AppBarLayout跟它的名字一樣,把容器內的組件全部作爲AppBar,就像這樣將一個ToolBar和TabLayout包裹在一起

這裏寫圖片描述

1.佈局實現

//引入degisn庫的支持
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:cardview-v7:23.4.0'
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zsy.appbarlayout.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recy"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.Toolbar
            android:id="@+id/bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/bar_color"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabSelectedTextColor="#1DACF9"
            app:tabTextColor="#808181" />
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

看下實現的效果
這裏寫圖片描述

2.一個可以滾動的組件,例如RecyclerView、ListView(這裏需要注意的是,貌似只支持RecyclerView、ListView,如果你用一個ScrollView,是沒有效果的)。如果:

1、給這個可滾動組件設置了layout_behavior
2、給另一個控件設置了layout_scrollFlags那麼,當設置了layout_behavior的控件滑動時,就會觸發設置了layout_scrollFlags的控件發生狀態的改變。
3.layout_scrollFlags具有以下幾個屬性

scroll: 所有想滾動出屏幕的view都需要設置這個flag沒有設置這個flag的view將被固定在屏幕頂部。
enterAlways: 這個flag讓任意向下的滾動都會導致該view變爲可見,啓用快速“返回模式”。
enterAlwaysCollapsed: 當你的視圖已經設置minHeight屬性又使用此標誌時,你的視圖只能已最小高度進入,
只有當滾動視圖到達頂部時才擴大到完整高度。

需要注意的是,後面兩種模式基本只有在CollapsingToolbarLayout纔有用,而前面兩種模式基本是需要一起使用的,
也就是說,這些flag的使用場景,基本已經固定了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章