Подзаголовок панели инструментов не отображается
Я установил свою панель инструментов в файле collapsingtoolbarlayout следующим образом:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
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">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Однако, когда я пытаюсь установить заголовок и субтитры, на панели инструментов появляется только заголовок!
private void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
if(toolbar != null){
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(mTitle);
getSupportActionBar().setSubtitle("Subtitle);
}
Как получить доступ к субтитрам панели инструментов?
Ответы
Ответ 1
Это ошибка CollapsingToolbarLayout.
Прочитайте http://saulmm.github.io/mastering-coordinator и посетите некоторые библиотеки:
https://github.com/anton46/WhatsApp-ProfileCollapsingToolbar
https://github.com/hendraanggrian/collapsingtoolbarlayout-subtitle
https://github.com/ahmadmuzakki/subtitle-collapsingtoolbar
https://github.com/k0shk0sh/CoordinatorLayoutExample
или любые другие, которые вам нравятся.
Также вы можете прочитать мой ответ о тенях в Android 4.4: fooobar.com/questions/45799/....
Ответ 2
Вы можете сделать это в макете XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:subtitle="ANDROID">
<!-- sub title for the Toolbar-->
</android.support.v7.widget.Toolbar>
Или вы можете сделать это программно:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // get the reference of Toolbar
setSupportActionBar(toolbar); // Setting/replace toolbar as the ActionBar4
toolbar.setSubtitle("ANDROID"); // setting a sub title for this Toolbar
getSupportActionBar().setDisplayShowTitleEnabled(false); // hide the current title from the Toolbar
Ответ 3
Попробуйте напрямую ссылаться на панель инструментов, а не пытаться установить заголовок/субтитры через getSupportActionBar(). Так что:
toolbar.setTitle()
toolbar.setSubtitle()
Заголовок/субтитры должны после этого работать надлежащим образом.