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?
This is a temporary solution. I didn't investigate the code deeply, but by disabling the refresh of Toolbar
in CollapsingToolbarLayout
, it worked.
Here is what I did:
public static void setRefreshToolbarEnable(CollapsingToolbarLayout collapsingToolbarLayout,
boolean refreshToolbarEnable) {
try {
Field field = CollapsingToolbarLayout.class.getDeclaredField("mRefreshToolbar");
field.setAccessible(true);
field.setBoolean(collapsingToolbarLayout, refreshToolbarEnable);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Simply set Collapsing toolbar 'titleEnabled=false' in xml Then Toolbar title will show up.
app:titleEnabled="false"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With