Marquee title in Toolbar / ActionBar in Android with Lollipop SDK?

后端 未结 5 1654
清歌不尽
清歌不尽 2021-01-04 19:38

I\'ve tried several different approaches, including the one found here (which in turn led me to trying both of the top answers to this question), as well as using reflection

5条回答
  •  难免孤独
    2021-01-04 20:15

    Kotlin solution to set MARQUEE for both Title and Subtitle TextViews (it just finds all TextViews inside Toolbar):

    findViewById(R.id.action_bar)?.let {
        setToolbarTextViewsMarquee(it)
    }
    
    fun setToolbarTextViewsMarquee(toolbar: Toolbar) {
        for (child in toolbar.children) {
            if (child is TextView) {
                setMarquee(child)
            }
        }
    }
    
    fun setMarquee(textView: TextView) {
        textView.ellipsize = TextUtils.TruncateAt.MARQUEE
        textView.isSelected = true
        textView.marqueeRepeatLimit = -1
    }
    

    So it's not necessary to add Toolbar view (android.support.v7.widget.Toolbar or androidx.appcompat.widget.Toolbar) to xml layout

    You can use the default Toolbar which AppCompat theme automatically adds: