自定义TextView 实现单行靠左多行靠右的特,并且不会出现间隔过大的情况

来源:互联网 发布:PHP parent 编辑:程序博客网 时间:2024/05/21 06:34

定义需求:要文字多行靠右,单行靠左,并且填充整个TextView,不出现空格的情况

实现思路:在定义TextView,借助一些属性做判断,动态设置文字出现的位置。

代码实现:

public class ManyTextView extends TextView  {    private int mLineY;    private int mViewWidth;    public ManyTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onLayout(boolean changed, int left, int top, int right,                            int bottom) {        super.onLayout(changed, left, top, right, bottom);    }    @Override    protected void onDraw(Canvas Canvas) {        TextPaint paint = getPaint();        paint.setColor(getCurrentTextColor());        paint.drawableState = getDrawableState();        mViewWidth = getMeasuredWidth();        String text = getText().toString();        mLineY = 0;        mLineY += getTextSize();        Layout layout = getLayout();                if (layout == null) {            return;        }        Paint.FontMetrics fm = paint.getFontMetrics();        int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));        textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout                .getSpacingAdd());                for (int i = 0; i < layout.getLineCount(); i++) {            int lineStart = layout.getLineStart(i);            int lineEnd = layout.getLineEnd(i);            float width = StaticLayout.getDesiredWidth(text, lineStart,                    lineEnd, getPaint());            String line = text.substring(lineStart, lineEnd);            if(i < layout.getLineCount() - 1) {                if (needScale(line)) {                    drawScaledText(Canvas, lineStart, line, width);                } else {                    Canvas.drawText(line, 0, mLineY, paint);                }            } else {                if(layout.getLineCount()==1){                    Canvas.drawText(line, getMeasuredWidth()-width, mLineY, paint);                }else{                    Canvas.drawText(line, 0, mLineY, paint);                }            }            mLineY += textHeight;        }    }    private void drawScaledText(Canvas Canvas, int lineStart, String line,                                float lineWidth) {        float x = 0;        if (isFirstLineOfParagraph(lineStart, line)) {            String blanks = "  ";            Canvas.drawText(blanks, x, mLineY, getPaint());            float bw = StaticLayout.getDesiredWidth(blanks, getPaint());            x += bw;            line = line.substring(3);        }        int gapCount = line.length() - 1;        int i = 0;        if (line.length() > 2 && line.charAt(0) == 12288                && line.charAt(1) == 12288) {            String substring = line.substring(0, 2);            float cw = StaticLayout.getDesiredWidth(substring, getPaint());            Canvas.drawText(substring, x, mLineY, getPaint());            x += cw;            i += 2;        }        float d = (mViewWidth - lineWidth) / gapCount;        for (; i < line.length(); i++) {            String c = String.valueOf(line.charAt(i));            float cw = StaticLayout.getDesiredWidth(c, getPaint());            Canvas.drawText(c, x, mLineY, getPaint());            x += cw + d;        }    }    private boolean isFirstLineOfParagraph(int lineStart, String line) {        return line.length() > 3 && line.charAt(0) == ' '                && line.charAt(1) == ' ';    }    private boolean needScale(String line) {        if (line == null || line.length() == 0) {            return false;        } else {            return line.charAt(line.length() - 1) != '\n';        }    }}
然后是在布局里面直接引用就可以了,分享至此就结束了,希望对各位有帮助

阅读全文
0 0