android 新控制項 AppBarLayout 使用

來源:互聯網
上載者:User

標籤: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的屬性有

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

為了使得Toolbar可以滑動,我們必須還得有個條件,就是CoordinatorLayout布局下包裹一個可以滑動的布局,比如 viewPager,RecyclerView,NestedScrollView(經過測試,ListView,ScrollView不支援)具有滑動效果的組件。並且給這些組件設定屬性,AppBarLayout 中可滑動的Toolbar可以滑動。

 

android 新控制項 AppBarLayout 使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.