TextView 跑马灯效果的坑

public class MarqueeTextView extends TextView {

    public MarqueeTextView(Context context) {
        super(context);
        setFocusable(true);//在每个构造方法中,将TextView设置为可获取焦点
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
    }

    @Override
    public boolean isFocused() {
        return true;
    }

}
  • 已经重写了TextView,强制设置获取focused,但是在xml文件中,我一旦设置了TextView的高度之后,跑马灯效果时有时无,甚是郁闷。
  • 本来效果好好的现在修改ui修改后就出现bug了,无奈之下,只有用排除法来一个个排查原因,直到把height设置成wrap_content之后,才豁然明了。

源码里面有这个提示,设置了height后会立即执行marquee效果,但是可能在 startMarquee()中界面未初始完成导致不执行跑马灯了。

if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
            if (!compressText(ellipsisWidth)) {
                final int height = mLayoutParams.height;
                // If the size of the view does not depend on the size of the text, try to
                // start the marquee immediately
                if (height != LayoutParams.WRAP_CONTENT && height != LayoutParams.MATCH_PARENT) {
                    startMarquee();
                } else {
                    // Defer the start of the marquee until we know our width (see setFrame())
                    mRestartMarquee = true;
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/shaoyangtangsong/article/details/83543557