Android TextView Justify Text

前端 未结 29 1920
滥情空心
滥情空心 2020-11-22 03:28

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it do

29条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 04:20

    I found a way to solve this problem, but this may not be very grace, but the effect is not bad.

    Its principle is to replace the spaces of each line to the fixed-width ImageSpan (the color is transparent).

    public static void justify(final TextView textView) {
    
        final AtomicBoolean isJustify = new AtomicBoolean(false);
    
        final String textString = textView.getText().toString();
    
        final TextPaint textPaint = textView.getPaint();
    
        final SpannableStringBuilder builder = new SpannableStringBuilder();
    
        textView.post(new Runnable() {
            @Override
            public void run() {
    
                if (!isJustify.get()) {
    
                    final int lineCount = textView.getLineCount();
                    final int textViewWidth = textView.getWidth();
    
                    for (int i = 0; i < lineCount; i++) {
    
                        int lineStart = textView.getLayout().getLineStart(i);
                        int lineEnd = textView.getLayout().getLineEnd(i);
    
                        String lineString = textString.substring(lineStart, lineEnd);
    
                        if (i == lineCount - 1) {
                            builder.append(new SpannableString(lineString));
                            break;
                        }
    
                        String trimSpaceText = lineString.trim();
                        String removeSpaceText = lineString.replaceAll(" ", "");
    
                        float removeSpaceWidth = textPaint.measureText(removeSpaceText);
                        float spaceCount = trimSpaceText.length() - removeSpaceText.length();
    
                        float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount;
    
                        SpannableString spannableString = new SpannableString(lineString);
                        for (int j = 0; j < trimSpaceText.length(); j++) {
                            char c = trimSpaceText.charAt(j);
                            if (c == ' ') {
                                Drawable drawable = new ColorDrawable(0x00ffffff);
                                drawable.setBounds(0, 0, (int) eachSpaceWidth, 0);
                                ImageSpan span = new ImageSpan(drawable);
                                spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            }
                        }
    
                        builder.append(spannableString);
                    }
    
                    textView.setText(builder);
                    isJustify.set(true);
                }
            }
        });
    }
    

    I put the code on GitHub: https://github.com/twiceyuan/TextJustification

    Overview:

提交回复
热议问题