CoordinatorLayout、AppBarLayout及控件扩展到状态栏

写在最前面:
1. 要想让某个控件扩展到状态栏,必须将该控件作为 AppBarLayout直接子控件,否则无效;
2. 如果要让该控件随着滚动而消失,必须设置layout_scrollFlag属性;
3. 当然,要让这一切在android4.4及以上生效,theme必须设置为AppCompat,最外层父控件必须为CoordinatorLayout。(纯属废话)
4. 注意fitSystemWindows属性的设置;如果android:fitSystemWindows=true,那么在window初始化该页面时,会调整对应空间的padding属性以适应系统的某些window(例如StatusBar);
5. 关于各种behavior和CoordinatorLayout详解

控件扩展到状态栏

  1. 主题设置
    ##style
    -- Base application theme. -->
    
    ##将对应的Activity主题设置为AppTheme.NoActionBar
    

    ##style21
    
  1. 布局
    <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"
    android:fitsSystemWindows="true"
    tools:context="cn.bupt.sse309.summary.master_detail_flow.ItemDetailActivity"
    tools:ignore="MergeRootFrame">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:foreground="#77000099"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_monkey"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" />

       
    android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/item_detail_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/large_text" />
    android.support.v4.widget.NestedScrollView>
    android.support.design.widget.CoordinatorLayout>

效果如下:

正如上图所示:当滑动时,ImageView总有一部分停留在状态栏,所以谷歌是这样干的:

    .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"
    android:fitsSystemWindows="true"
    tools:context="cn.bupt.sse309.summary.master_detail_flow.ItemDetailActivity"
    tools:ignore="MergeRootFrame">

    .support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:foreground="#77000099"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        .support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_monkey"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@color/colorPrimaryDark"

            app:toolbarId="@+id/detail_toolbar">

            .support.v7.widget.Toolbar
                android:id="@+id/detail_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


        .support.design.widget.CollapsingToolbarLayout>

    .support.design.widget.AppBarLayout>

    .support.v4.widget.NestedScrollView
        android:id="@+id/item_detail_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        "match_parent"
            android:layout_height="match_parent"
            android:text="@string/large_text" />
    .support.v4.widget.NestedScrollView>
    
    .support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/stat_notify_chat"
        app:layout_anchor="@+id/item_detail_container"
        app:layout_anchorGravity="top|end" />

.support.design.widget.CoordinatorLayout>

也就是,当AppBarLayout下的控件滑动到底部时,让toolbar现实出来,并设置Statusbar的颜色,效果如下。
CoordinatorLayout、AppBarLayout及控件扩展到状态栏_第1张图片

你可能感兴趣的:(Andrid)