2017-04-01 142 views
0

我目前在佈局的上半部分使用CollapsingToolbar佈局。我想匹配特定部分(僅限LinearLayout),並將工具欄固定在頂部。我能夠將工具欄位置固定在另一個項目的頂部,並防止崩潰。但是,由於某種原因,我無法修復我的工具欄的位置。任何幫助將不勝感激。這裏是我的佈局的XML:Android - 無法修復摺疊工具欄佈局中的工具欄

<?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:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/profile_coordinate_layout" 
> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <android.support.design.widget.CollapsingToolbarLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|enterAlways" 
     android:layout_gravity="fill_vertical" 
     android:fitsSystemWindows="true" 
     app:layout_collapseMode="pin" 
     app:expandedTitleMarginStart="?actionBarSize" 
     app:contentScrim="?colorPrimary" 
     > 

     <!--|||||||||||||||||||||||||||||||||||||||||||||||||--> 
     <!--|||||||||||||||Collection Profile and about||||||--> 
     <!--|||||||||||||||||||||||||||||||||||||||||||||||||--> 
     <LinearLayout 
      android:id="@+id/ll_header" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="?actionBarSize" 
      android:orientation="vertical" 
      app:layout_collapseMode="pin" 
      android:background="@drawable/gradient_feed_bg" 
      > 
      <include 
       layout="@layout/item_collection_profile" /> 
     </LinearLayout> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?actionBarSize" 
      android:background="@drawable/dark_primary_no_radius" 
      app:layout_collapseMode="pin" 
      android:elevation="@dimen/margin_4dp" 
      > 
      <include layout="@layout/action_bar_collection_profile"/> 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 


<android.support.v4.view.ViewPager 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    ></android.support.v4.view.ViewPager> 
</android.support.design.widget.CoordinatorLayout> 

我在ViewPager上加載另一個片段。我正在使用下面的代碼來加載工具欄。

findViewById(R.id.toolbar). setBackgroundResource(R.drawable.dark_primary_no_radius); 
setSupportActionBar((android.support.v7.widget.Toolbar)findViewById(R.id.toolbar)); 

這裏是修訂結果:

enter image description here

正如你所看到的,在第二圖像中,工具欄倒在上面。任何幫助將不勝感激。

謝謝。

回答

0

您只需在工具欄中使用app:layout_collapseMode="pin"。 從其他組件(如LinearLayout和CollapsingToolbarLayout)中刪除此項。

<android.support.design.widget.AppBarLayout 
android:id="@+id/app_bar_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
> 
<android.support.design.widget.CollapsingToolbarLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed" 
    android:layout_gravity="fill_vertical" 
    android:fitsSystemWindows="true" 
    app:expandedTitleMarginStart="?actionBarSize" 
    app:contentScrim="?colorPrimary" 
    > 

    <!--|||||||||||||||||||||||||||||||||||||||||||||||||--> 
    <!--|||||||||||||||Collection Profile and about||||||--> 
    <!--|||||||||||||||||||||||||||||||||||||||||||||||||--> 
    <LinearLayout 
     android:id="@+id/ll_header" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?actionBarSize" 
     android:orientation="vertical" 
     app:layout_collapseMode="parallax" 
     android:background="@drawable/gradient_feed_bg" 
     > 
     <include 
      layout="@layout/item_collection_profile" /> 
    </LinearLayout> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?actionBarSize" 
     android:background="@drawable/dark_primary_no_radius" 
     app:layout_collapseMode="pin" 
     android:elevation="@dimen/margin_4dp" 
     > 
     <include layout="@layout/action_bar_collection_profile"/> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.CollapsingToolbarLayout> 

+0

我做到了。但是,工具欄仍在崩潰。 –

+0

謝謝你,它解決了我的問題,工具欄被固定在頂部,但是在工具欄的底部有一個大小爲空的工具欄。我該如何解決這個問題? –

+0

如果我使用app:layout_scrollFlags =「scroll | exitUntilCollapsed」,那麼我在底部有一個空白的地方,如果我使用app:layout_scrollFlags =「scroll | enterAlways」,那麼工具欄不固定在頂部。 –

相關問題