当我在CoordinatorLayout中滚动时,如何保持固定的工具栏?

问题描述:

滚动到CollapsingToolbarLayout时,我该如何保持固定的Toolbar。贝娄是我的xml:当我在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" 
    xmlns:card_view="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.support.design.widget.AppBarLayout 
     android:id="@+id/receipt_editor_appbarlayout" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/product_editor_appbarlayout_height" 
     app:elevation="0dp"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:contentScrim="@color/colorPrimary" 
      app:titleEnabled="false" 
      app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:id="@+id/receipt_picture_imageview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginTop="?attr/actionBarSize" 
        android:scaleType="centerCrop" 
        app:layout_collapseMode="parallax" 
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" /> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@drawable/gradient" 
        android:paddingBottom="5dp"> 

        <com.joanzapata.iconify.widget.IconButton 
         android:id="@+id/icon_btn_iconCamera" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_marginLeft="10dp" 
         android:background="@null" 
         android:text="@string/icon_fa_camera" 
         android:textColor="#ffffff" /> 


        <com.joanzapata.iconify.widget.IconButton 
         android:id="@+id/icon_btn_select_image_gallery" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_marginRight="10dp" 
         android:background="@null" 
         android:text="@string/icon_material_collections" 
         android:textColor="#ffffff" /> 
       </RelativeLayout> 

      </RelativeLayout> 


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

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <ImageButton 
         android:id="@+id/back_imagebutton" 
         style="@style/ToolbarButton" 
         android:layout_alignParentRight="true" 
         app:srcCompat="@drawable/ic_product_editor_back" /> 

        <TextView 
         android:id="@+id/toolbar_title_textview" 
         fontPath="@string/bold_font_path" 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_toLeftOf="@id/back_imagebutton" 
         android:gravity="center_vertical" 
         android:textAppearance="?android:textAppearanceMedium" 
         android:textColor="@color/colorTextLight" /> 

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

     </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"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="17.29dp"> 

      <android.support.v7.widget.CardView 
       android:id="@+id/cv_register_shipping_receipt_spinner" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp" 
       app:cardElevation="0.5dp"> 

       <Spinner 
        android:id="@+id/shipping_methods_spinner" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:entries="@array/shipping_methods" /> 

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

      <RelativeLayout 
       android:id="@+id/post_tracking_code_field_container_relativelayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/cv_register_shipping_receipt_spinner" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp" 
       android:background="#F7F7F7"> 

       <RelativeLayout 
        android:id="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:background="#FFFFFF" 
        android:gravity="center_vertical"> 


        <EditText 
         android:id="@+id/post_tracking_code_edittext" 
         style="@style/ProductEditorEditText" 
         android:layout_width="match_parent" 
         android:layout_height="45dp" 
         android:layout_toRightOf="@+id/scan_post_tracking_code_iconbutton" 
         android:background="@null" 
         android:gravity="center_vertical" 
         android:hint="@string/tracking_code" 
         android:inputType="number" 
         android:maxLength="32" /> 

        <com.joanzapata.iconify.widget.IconButton 
         android:id="@+id/scan_post_tracking_code_iconbutton" 
         android:layout_width="125dp" 
         android:layout_height="40dp" 
         android:layout_alignParentLeft="true" 
         android:layout_marginBottom="3dp" 
         android:layout_marginLeft="3dp" 
         android:layout_marginRight="3dp" 
         android:layout_marginTop="3dp" 
         android:background="@drawable/drawable_button_background" 
         android:text="@string/icon_fa_barcodes" 
         android:textAppearance="?android:textAppearanceMedium" 
         android:textColor="@color/colorTextLight" /> 
       </RelativeLayout> 

       <TextView 
        android:id="@+id/post_cost_field_textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_below="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
        android:layout_marginTop="8dp" 
        android:visibility="gone" /> 

       <TextView 
        android:id="@+id/post_cost_field_textviewTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_below="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
        android:layout_marginTop="8dp" 
        android:text="@string/cost_post" 
        android:visibility="gone" /> 

       <TextView 
        android:id="@+id/post_date_field_textviewTitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_below="@+id/post_cost_field_textview" 
        android:layout_marginTop="8dp" 
        android:text="@string/shipping_date_time" 
        android:visibility="gone" /> 

       <TextView 
        android:id="@+id/post_date_field_textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_below="@+id/post_cost_field_textview" 
        android:layout_marginTop="8dp" 
        android:visibility="gone" /> 

       <EditText 
        android:id="@+id/post_cost_edittext" 
        style="@style/ProductEditorEditText" 
        android:layout_width="match_parent" 
        android:layout_height="45dp" 
        android:layout_below="@+id/post_date_field_textview" 
        android:layout_marginTop="8dp" 
        android:background="@drawable/drawable_edittext_background" 
        android:gravity="center_vertical" 
        android:hint="@string/final_cost_to_rial" 
        android:inputType="number" 
        android:maxLength="32" 
        android:visibility="gone" /> 

       <android.support.v7.widget.CardView 
        android:id="@+id/card_view_time_send" 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:layout_below="@+id/post_cost_edittext" 
        android:layout_marginTop="8dp" 
        android:visibility="gone" 
        card_view:cardElevation="1dp"> 

        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 

         <TextView 
          android:id="@+id/post_times_dates_textview_handle" 
          style="@style/ProductEditorEditText" 
          android:layout_width="match_parent" 
          android:layout_height="45dp" 
          android:layout_toRightOf="@+id/calender_iconbutton" 
          android:background="@null" 
          android:gravity="center_vertical" 
          android:hint="@string/shipping_date_time" 
          android:maxLength="32" /> 

         <com.joanzapata.iconify.widget.IconButton 
          android:id="@+id/calender_iconbutton" 
          android:layout_width="50dp" 
          android:layout_height="50dp" 
          android:layout_alignParentLeft="true" 
          android:layout_marginBottom="5dp" 
          android:layout_marginLeft="5dp" 
          android:layout_marginRight="5dp" 
          android:layout_marginTop="5dp" 
          android:background="@null" 
          android:text="@string/icon_material_event" 
          android:textAppearance="?android:textAppearanceMedium" 
          android:textColor="@color/grays" /> 
        </RelativeLayout> 
       </android.support.v7.widget.CardView> 

       <EditText 
        android:id="@+id/post_receipt_description_edittext" 
        style="@style/ProductEditorEditText" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/card_view_time_send" 
        android:layout_marginTop="8dp" 
        android:background="@drawable/drawable_edittext_background" 
        android:gravity="top|right" 
        android:hint="@string/shipping_receipt_description" 
        android:inputType="textMultiLine" 
        android:minLines="3" 
        android:visibility="gone" /> 

       <android.support.v7.widget.AppCompatButton 
        android:id="@+id/register_post_receipt_button" 
        fontPath="@string/bold_font_path" 
        android:layout_width="match_parent" 
        android:layout_height="60dp" 
        android:layout_below="@+id/post_receipt_description_edittext" 
        android:layout_marginTop="8dp" 
        android:background="@color/alto" 
        android:enabled="false" 
        android:text="@string/checking_tracking_code" 
        android:textColor="@color/colorTextLight" /> 

      </RelativeLayout> 
     </RelativeLayout> 
    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 
+0

到位工具栏上的AppBarLayout外 –

解决我的问题:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:fitsSystemWindows="true" 
     app:layout_collapseMode="pin"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageButton 
       android:id="@+id/back_imagebutton" 
       style="@style/ToolbarButton" 
       android:layout_alignParentRight="true" 
       app:srcCompat="@drawable/ic_product_editor_back" /> 

      <TextView 
       android:id="@+id/toolbar_title_textview" 
       fontPath="@string/bold_font_path" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_toLeftOf="@id/back_imagebutton" 
       android:gravity="center_vertical" 
       android:textAppearance="?android:textAppearanceMedium" 
       android:textColor="@color/colorTextLight" /> 

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

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar"> 


     <android.support.design.widget.AppBarLayout 
      android:id="@+id/receipt_editor_appbarlayout" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/product_editor_appbarlayout_height" 
      app:elevation="0dp"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:contentScrim="@color/colorPrimary" 
       app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed" 
       app:titleEnabled="false"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <ImageView 
         android:id="@+id/receipt_picture_imageview" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:scaleType="centerCrop" 
         app:layout_collapseMode="parallax" 
         app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" /> 

        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_alignParentBottom="true" 
         android:background="@drawable/gradient" 
         android:paddingBottom="5dp"> 

         <com.joanzapata.iconify.widget.IconButton 
          android:id="@+id/icon_btn_iconCamera" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentLeft="true" 
          android:layout_marginLeft="10dp" 
          android:background="@null" 
          android:text="@string/icon_fa_camera" 
          android:textColor="#ffffff" /> 


         <com.joanzapata.iconify.widget.IconButton 
          android:id="@+id/icon_btn_select_image_gallery" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_alignParentRight="true" 
          android:layout_marginRight="10dp" 
          android:background="@null" 
          android:text="@string/icon_material_collections" 
          android:textColor="#ffffff" /> 
        </RelativeLayout> 

       </RelativeLayout> 

      </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"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginTop="17.29dp"> 

       <android.support.v7.widget.CardView 
        android:id="@+id/cv_register_shipping_receipt_spinner" 
        android:layout_width="match_parent" 
        android:layout_height="40dp" 
        android:layout_marginBottom="10dp" 
        android:layout_marginLeft="16dp" 
        android:layout_marginRight="16dp" 
        app:cardElevation="0.5dp"> 

        <Spinner 
         android:id="@+id/shipping_methods_spinner" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:entries="@array/shipping_methods" /> 

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

       <RelativeLayout 
        android:id="@+id/post_tracking_code_field_container_relativelayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/cv_register_shipping_receipt_spinner" 
        android:layout_marginLeft="16dp" 
        android:layout_marginRight="16dp" 
        android:background="#F7F7F7"> 

        <RelativeLayout 
         android:id="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
         android:layout_width="match_parent" 
         android:layout_height="50dp" 
         android:background="#FFFFFF" 
         android:gravity="center_vertical"> 


         <EditText 
          android:id="@+id/post_tracking_code_edittext" 
          style="@style/ProductEditorEditText" 
          android:layout_width="match_parent" 
          android:layout_height="45dp" 
          android:layout_toRightOf="@+id/scan_post_tracking_code_iconbutton" 
          android:background="@null" 
          android:gravity="center_vertical" 
          android:hint="@string/tracking_code" 
          android:inputType="number" 
          android:maxLength="32" /> 

         <com.joanzapata.iconify.widget.IconButton 
          android:id="@+id/scan_post_tracking_code_iconbutton" 
          android:layout_width="125dp" 
          android:layout_height="40dp" 
          android:layout_alignParentLeft="true" 
          android:layout_marginBottom="3dp" 
          android:layout_marginLeft="3dp" 
          android:layout_marginRight="3dp" 
          android:layout_marginTop="3dp" 
          android:background="@drawable/drawable_button_background" 
          android:text="@string/icon_fa_barcodes" 
          android:textAppearance="?android:textAppearanceMedium" 
          android:textColor="@color/colorTextLight" /> 
        </RelativeLayout> 

        <TextView 
         android:id="@+id/post_cost_field_textview" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_below="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
         android:layout_marginTop="8dp" 
         android:visibility="gone" /> 

        <TextView 
         android:id="@+id/post_cost_field_textviewTitle" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_below="@+id/cv_register_shipping_receipt_edtPost_trackingCode" 
         android:layout_marginTop="8dp" 
         android:text="@string/cost_post" 
         android:visibility="gone" /> 

        <TextView 
         android:id="@+id/post_date_field_textviewTitle" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_below="@+id/post_cost_field_textview" 
         android:layout_marginTop="8dp" 
         android:text="@string/shipping_date_time" 
         android:visibility="gone" /> 

        <TextView 
         android:id="@+id/post_date_field_textview" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_below="@+id/post_cost_field_textview" 
         android:layout_marginTop="8dp" 
         android:visibility="gone" /> 

        <EditText 
         android:id="@+id/post_cost_edittext" 
         style="@style/ProductEditorEditText" 
         android:layout_width="match_parent" 
         android:layout_height="45dp" 
         android:layout_below="@+id/post_date_field_textview" 
         android:layout_marginTop="8dp" 
         android:background="@drawable/drawable_edittext_background" 
         android:gravity="center_vertical" 
         android:hint="@string/final_cost_to_rial" 
         android:inputType="number" 
         android:maxLength="32" 
         android:visibility="gone" /> 

        <android.support.v7.widget.CardView 
         android:id="@+id/card_view_time_send" 
         android:layout_width="match_parent" 
         android:layout_height="50dp" 
         android:layout_below="@+id/post_cost_edittext" 
         android:layout_marginTop="8dp" 
         android:visibility="gone" 
         card_view:cardElevation="1dp"> 

         <RelativeLayout 
          android:layout_width="match_parent" 
          android:layout_height="match_parent"> 

          <TextView 
           android:id="@+id/post_times_dates_textview_handle" 
           style="@style/ProductEditorEditText" 
           android:layout_width="match_parent" 
           android:layout_height="45dp" 
           android:layout_toRightOf="@+id/calender_iconbutton" 
           android:background="@null" 
           android:gravity="center_vertical" 
           android:hint="@string/shipping_date_time" 
           android:maxLength="32" /> 

          <com.joanzapata.iconify.widget.IconButton 
           android:id="@+id/calender_iconbutton" 
           android:layout_width="50dp" 
           android:layout_height="50dp" 
           android:layout_alignParentLeft="true" 
           android:layout_marginBottom="5dp" 
           android:layout_marginLeft="5dp" 
           android:layout_marginRight="5dp" 
           android:layout_marginTop="5dp" 
           android:background="@null" 
           android:text="@string/icon_material_event" 
           android:textAppearance="?android:textAppearanceMedium" 
           android:textColor="@color/grays" /> 
         </RelativeLayout> 
        </android.support.v7.widget.CardView> 

        <EditText 
         android:id="@+id/post_receipt_description_edittext" 
         style="@style/ProductEditorEditText" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_below="@+id/card_view_time_send" 
         android:layout_marginTop="8dp" 
         android:background="@drawable/drawable_edittext_background" 
         android:gravity="top|right" 
         android:hint="@string/shipping_receipt_description" 
         android:inputType="textMultiLine" 
         android:minLines="3" 
         android:visibility="gone" /> 

        <android.support.v7.widget.AppCompatButton 
         android:id="@+id/register_post_receipt_button" 
         fontPath="@string/bold_font_path" 
         android:layout_width="match_parent" 
         android:layout_height="60dp" 
         android:layout_below="@+id/post_receipt_description_edittext" 
         android:layout_marginTop="8dp" 
         android:background="@color/alto" 
         android:enabled="false" 
         android:text="@string/checking_tracking_code" 
         android:textColor="@color/colorTextLight" /> 

       </RelativeLayout> 
      </RelativeLayout> 
     </android.support.v4.widget.NestedScrollView> 

    </android.support.design.widget.CoordinatorLayout> 
</RelativeLayout>