I'm creating a layout using a CoordinatorLayout
holding an AppBarLayout
with a CollapsableToolbarLayout
and a Toolbar
. Below the app bar is a RecyclerView
.
When scrolling in the RecyclerView
, the Toolbar
should collapse, similar to the CheeseSquare demo app. The difference from that demo is that 1) I want my Toolbar
(including the title) always statically at the top (so I've set app:titleEnabled
on my CTL
), and 2) I don't want to collapse the app bar fully into only showing the single line Toolbar
since I have some text views etc below the Toolbar
which the app bar should never collapse beyond. I am using a custom Behavior
to control these text views during collapsing, and all this works fine.
Problem is, I don't seem to grasp how to best set the anchor point at which the collapsing will end. I know that the app bar will collapse until it snaps to to the height of the Toolbar
. So I would need to either put my custom views inside the toolbar, hence increasing the height of the Toolbar
itself, or somehow instruct the CTL
to anchor to my custom view instead. However, whenever I change the height of the Toolbar
, the title text gets strangely aligned and I haven't found a good way of keeping it properly aligned with any set of reasonable attributes.
Question is, should I be playing with the height of the Toolbar
at all, or is there a way of getting the CTL
to stop collapsing at some other anchor point than the bottom of the Toolbar
?
Or should I indeed modify the height of the Toolbar
and place my own layout in it that includes the title text, and manually make sure to align it vertically with the toolbar icons etc? The best I've managed so far is to place a LinearLayout
containing a TextView
inside the Toolbar
, with a hard-coded layout_marginTop="14dp"
, which might have compat issues. It seems really strange that it wouldn't be possible to increase the height of a Toolbar
while still maintaining the default title text position.
Or should I not use the CTL
at all but instead create my completely custom behavior also for the parts that collapses the app bar during initial RecyclerView
scroll events?
I've searched for similar questions obviously, and the closest I've found is this question which does not address my problem with the title text getting messed up.
My current layout, including the ugly hard-coded manual title TextView
inside the Toolbar
:
<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="180dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<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"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="110dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="14dp">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"/ >
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:elevation="10dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_anchor="@id/toolbar"
app:layout_behavior="my behavior class">
<!-- My custom views to be shown in the app bar, animated through custom behavior -->
</LinearLayout>
<android.support.v7.widget.RecyclerView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>