標籤:ext 返回 roi odi dsc select end 沒有 actionbar
AppBarLayout 是繼承LinerLayout實現的一個ViewGroup容器組件,它是為了Material Design設計的App Bar,支援手勢滑動操作。
預設的AppBarLayout是垂直方向的,它的作用是把AppBarLayout包裹的內容都作為AppBar。代碼布局如下:
<android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize"></android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll" app:tabIndicatorColor="@android:color/holo_red_dark" app:tabSelectedTextColor="@android:color/holo_red_dark" app:tabTextColor="@android:color/black" /></android.support.design.widget.AppBarLayout>
此處將Toolbar 和Tablayout的組合部分共同構成 AppBar的效果。 AppBarLayout必須作為Toolbar的父版面配置容器。AppBarLayout支援手勢滑動效果的,要跟CoordinatorLayout配合使用。
<?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" android:id="@+id/main_content" 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.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" /> </android.support.design.widget.AppBarLayout> <!--可滑動的布局內容--> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_discuss" android:layout_gravity="bottom|end"/></android.support.design.widget.CoordinatorLayout>
app:layout_scrollFlags=”scroll|enterAlways” 屬性來確定哪個組件是可滑動的。layout_scrollFlags的屬性有
- scroll: 所有想滾動出螢幕的view都需要設定這個flag- 沒有設定這個flag的view將被固定在螢幕頂部。
- enterAlways: 這個flag讓任意向下的滾動都會導致該view變為可見,啟用快速“返回模式”。
- enterAlwaysCollapsed: 當你的視圖已經設定minHeight屬性又使用此標誌時,你的視圖只能已最小高度進入,只有當滾動視圖到達頂部時才擴大到完整高度。
- exitUntilCollapsed: 滾動退出螢幕,最後摺疊在頂端。
為了使得Toolbar可以滑動,我們必須還得有個條件,就是CoordinatorLayout布局下包裹一個可以滑動的布局,比如 viewPager,RecyclerView,NestedScrollView(經過測試,ListView,ScrollView不支援)具有滑動效果的組件。並且給這些組件設定屬性,AppBarLayout 中可滑動的Toolbar可以滑動。
android 新控制項 AppBarLayout 使用