Android collapsing toolbar: How to resize the text, so that it shows full text than partially

本秂侑毒 提交于 2019-12-01 19:54:05

First define your text styles in styles.xml

<style name="TextAppearance.MyApp.Title.Collapsed" parent="android:TextAppearance">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">11sp</item>
</style>

<style name="TextAppearance.MyApp.Title.Expanded" parent="android:TextAppearance">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">14sp</item>
</style>

Note that the values are just examples; you need to change them to fit your app. Also, you may want a different TextAppearance style as a parent.

Then in XML :

<android.support.design.widget.CollapsingToolbarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    .
    .
    .
    app:collapsedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Collapsed"
    app:expandedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Expanded"


/>

in code:

collapsingToolbar.setCollapsedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Collapsed);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Expanded);

EDIT: In the comments there is a discussion about multi-line text. CollapsingToolbarLayout does not support multi-line text. Please ignore my suggestion to use setCustomView() on the toolbar. According to docs:

Do not manually add views to the Toolbar at run time. We will add a 'dummy view' to the Toolbar which allows us to work out the available space for the title. This can interfere with any views which you add.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!