I used CollapsingToolbarLayout
as the parent of Toolbar
, below it the layout
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/test_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"
app:theme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Then I want to set the title of the Toolbar
with the following code, but it didn't work. The title just didn't show.
Toolbar toolbar = (Toolbar) findViewById(R.id.test_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("ABC");
I also tried set the title in CollapsingToolbarLayout
with the following code, it didn't work either.
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
collapsingToolbarLayout.setTitleEnabled(true);
collapsingToolbarLayout.setTitle("ABC");
But if I removed CollapsingToolbarLayout
from my layout and make AppBarLayout
as the direct parent of Toolbar
, the code above to set the title of Toolbar
worked.
Did I missed something? This issue is so weird. Is it a bug in design support library? How can I solve it without changing my layout?
android:fitsSystemWindows="true"
on yourCoordinatorLayout
,AppBarLayout
andCollapsingToolbarLayout
. – Mllly