Android TextView Justify Text

前端 未结 29 1175
滥情空心
滥情空心 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:15

    I think there are two options:

    • Use something like Pango that specializes in this via the NDK and render text to an OpenGL or other surface.

    • Use Paint.measureText() and friends to get the lengths of words and lay them out manually on a Canvas in a custom view.

    0 讨论(0)
  • 2020-11-22 04:16

    This line of code will make your text justified

       android:justificationMode="inter_word"
    
    0 讨论(0)
  • 2020-11-22 04:17

    You have to set

    android:layout_height="wrap_content"
    

    and

    android:layout_centerInParent="true"
    
    0 讨论(0)
  • 2020-11-22 04:19

    FILL_HORIZONTAL is equivalent to CENTER_HORIZONTAL. You can see this code snippet in textview's source code:

    case Gravity.CENTER_HORIZONTAL:
    case Gravity.FILL_HORIZONTAL:
        return (mLayout.getLineWidth(0) - ((mRight - mLeft) -
                getCompoundPaddingLeft() - getCompoundPaddingRight())) /
                getHorizontalFadingEdgeLength();
    
    0 讨论(0)
  • 2020-11-22 04:19

    This doesn't really justify your text but

    android:gravity="center_horizontal"
    

    is the best choice you have.

    0 讨论(0)
  • 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:

    0 讨论(0)
提交回复
热议问题