Android TextView 设置删除线

百度了一下,大多数都是这样的:

textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

但实际使用无效。后来换成Google搜索:

设置删除线代码:

textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
取消删除线代码:

textView.setPaintFlags(textView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
测试达到了期望的效果,Google就是强大。

查看TextView源代码:

/**
     * Sets flags on the Paint being used to display the text and
     * reflows the text if they are different from the old flags.
     * @see Paint#setFlags
     */
    @android.view.RemotableViewMethod
    public void setPaintFlags(int flags) {
        if (mTextPaint.getFlags() != flags) {
            mTextPaint.setFlags(flags);

            if (mLayout != null) {
                nullLayouts();
                requestLayout();
                invalidate();
            }
        }
    }

/**
     * @return the base paint used for the text.  Please use this only to
     * consult the Paint's properties and not to change them.
     */
    public TextPaint getPaint() {
        return mTextPaint;
    }

由代码可知,setPaintFlags()有刷新过程。而getPaint()返回TextPaint,此类继承于Paint,Paint.setFlags()是native函数,且TextPaint没有对其进行复写,无法进一步分析原因了。



你可能感兴趣的:(Android)