Autoscrollable title in Android Actionbar (Marquee

2019-01-26 16:07发布

is there a possibility to make the title of an Android ActionBar scroll automatically (marquee) if it's too large?

2条回答
劫难
2楼-- · 2019-01-26 16:33

You could try implementing my answer to this question and add android:ellipsize="marquee" to the TextView...? Worth a shot.

查看更多
Lonely孤独者°
3楼-- · 2019-01-26 16:58

Well I have done this way:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

    try {
        Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
        f.setAccessible(true);
        TextView toolbarTextView = (TextView) f.get(toolbar);
        toolbarTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        toolbarTextView.setFocusable(true);
        toolbarTextView.setFocusableInTouchMode(true);
        toolbarTextView.requestFocus();
        toolbarTextView.setSingleLine(true);
        toolbarTextView.setSelected(true);
        toolbarTextView.setMarqueeRepeatLimit(-1);

        // set text on Textview

        toolbarTextView.setText("Hello Android ! This is a sample marquee text. That's great. Enjoy");
    } catch (NoSuchFieldException e) {
    } catch (IllegalAccessException e) {
    }

Hope this will help you.

查看更多
登录 后发表回答