2016-09-17 27 views
1

我有這種佈局,當這個區域移動到頂部時(只是在工具欄下方),紅色區域必須被修復。其餘的(在這個紅色區域之上)可以移動到頂部。Android:CoordinatorLayout - 如何在頂部屏幕上有一個修復標題視圖?

enter image description here

佈局:

<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:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/detail_backdrop_height" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginEnd="64dp"> 

     <ImageView 
      android:id="@+id/backdrop" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" /> 

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

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

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

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <!-- This view MUST STOP on the top of screen (below toobar) 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:background="#ff6677" 
      android:layout_marginBottom="24dp"/> 

     ... rest of view 

結果應該是這樣的:

enter image description here

回答

0

嘗試把你的觀點,你需要在AppBarLayout以保持靜態的: 佈局:

<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:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
android:id="@+id/appbar" 
android:layout_width="match_parent" 
android:layout_height="@dimen/detail_backdrop_height" 
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.CollapsingToolbarLayout 
    android:id="@+id/collapsing_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed" 
    android:fitsSystemWindows="true" 
    app:contentScrim="?attr/colorPrimary" 
    app:expandedTitleMarginStart="48dp" 
    app:expandedTitleMarginEnd="64dp"> 

    <ImageView 
     android:id="@+id/backdrop" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop" 
     android:fitsSystemWindows="true" 
     app:layout_collapseMode="parallax" /> 

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

</android.support.design.widget.CollapsingToolbarLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"/> // THIS WILL BE FIXED ON TOP 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
相關問題