Android Actionbar中的自动滚动标题(Marquee)

5

如果Android ActionBar的标题太长,是否有可能使其自动滚动(跑马灯)?

2个回答

4

我是这样做的:

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) {
    }

希望这可以帮到你。

3
你可以尝试实现我对这个问题的回答,并将android:ellipsize="marquee"添加到TextView中...?值得一试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接