How I can multiline the expanded title of CollapsingToolbarLayout?

*爱你&永不变心* 提交于 2019-12-03 10:45:40

问题


My problem is the next. I would use relatively large texts as CollapsingToolbarLayout title so I need to show it as multiline mode. When I try to change text appearance through the setExpandedTitleTextAppearance() method it doesn't work. The code who I used is the next:

<style name="ToolbarExpandedTitle">
    <item name="android:textSize">48sp</item>
    <item name="android:shadowColor">#ffffff</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:singleLine">false</item>
    <item name="android:minLines">3</item>
    <item name="android:lines">4</item>
    <item name="android:maxLines">5</item>
</style>

I would like to see the title on multiple lines instead of showing the ellipsis.


回答1:


Checkout this library multiline-collapsingtoolbar.

multiline-collapsingtoolbar is a replacement for CollapsingToolbarLayout from the Android Design Support Library which can deal with multiline titles (currently hard-coded to a maximum of 3 lines) in the expanded state. When collapsing the toolbar, the lower lines of the title fade away to leave only the top line visible.

As the Design Support Library, it should be compatible with API 7 (Android 2.1) and above




回答2:


This doesn't seem to be supported at present. A possible workaround is to use your own view and hide the toolbar title when expanded. One approach to do this is here:

https://stackoverflow.com/a/31529101/834692

Hopefully a future version of the Support Library will add this.




回答3:


A simpler approach would be to add a custom layout added to the CollapsingToolbar and pin it.

Some of the notes to take care:

  • The the toolbar should have the height of your collapsed content, this can be done by code or at the XML.
  • The elements you want to hide must be BEFORE the toolbar definition in the layout XML file
  • The elements you want to remain visible must be AFTER the toolbar definition in the layout XML file.

Checkout the full code here. The final result is:




回答4:


I did this with help from this library

https://github.com/opacapp/multiline-collapsingtoolbar

add the library to your project and replace the CollapsingToolbarLayout with net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout



来源:https://stackoverflow.com/questions/31437685/how-i-can-multiline-the-expanded-title-of-collapsingtoolbarlayout

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