0

this教程中,我有这个CollapsingToolbarLayout使用CollapsingToolbarLayout,我怎样才能将其设置为支持操作栏?

<android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"> 

     <ImageView 
      android:id="@+id/blurred_background" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:ignore="ContentDescription" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/defaultimage" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.7" 
      tools:ignore="ContentDescription" 
      android:id="@+id/profilePicture" /> 


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

不过,我需要使用setSupportActionBar()这样我就可以使用setTitle()setHomeAsUpEnabled()等方法。但是,显然CollapsingToolbarLayout不能投射到Toolbar,并且setSupportActionBar()方法只接受Toolbar方法。

是否有替代方法来设置它?

+0

通过https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout – Pavya

+0

为什么不把你的'ImageView'包装在'Toolbar'中? –

+0

@MikeM。不错的想法,但它似乎像我的'ImageView'失去了他们的对齐。 (其中一个居中,现在它在屏幕的左侧) –

回答

0

尝试做这样的事情:

<android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginBottom="70dp" 
     app:expandedTitleMarginEnd="10dp" 
     app:expandedTitleMarginStart="48dp" 
     app:layout_collapseParallaxMultiplier="0.5" 
     app:layout_scrollFlags="scroll|enterAlways|snap"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="160dp" 
      android:gravity="bottom" 
      android:orientation="vertical" 
      app:layout_collapseMode="parallax"> 

      <ImageView 
       android:id="@+id/blurred_background" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:ignore="ContentDescription" /> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true" 
       android:src="@drawable/defaultimage" 
       app:layout_collapseMode="parallax" 
       app:layout_collapseParallaxMultiplier="0.7" 
       tools:ignore="ContentDescription" 
       android:id="@+id/profilePicture" /> 
     </LinearLayout> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      app:contentInsetLeft="0dp" 
      app:contentInsetStart="0dp" 
      app:contentInsetStartWithNavigation="0dp" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" /> 
    </android.support.design.widget.CollapsingToolbarLayout> 

添加的LinearLayout/RelativeLayout的内部图像的意见,并添加工具栏里面CollapsingToolbarLayout实现工具栏的功能。