Android TextView Justify Text

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

    Simply try this,

            <TextView
            .......................................
            .......................................
            .......................................
            android:layout_gravity="center_vertical|end"
            android:justificationMode="inter_word"
            .......................................
            .......................................
            />
    

    Hope this help!

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

    There is a CustomView for this problem, this custom text view is support Justified Text View.

    Loot at this: JustifiedTextView

    import java.util.ArrayList;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Typeface;
    import android.text.TextPaint;
    import android.view.View;
    
    public class JustifiedTextView extends View {
            String text;
            ArrayList<Line> linesCollection = new ArrayList<Line>();
            TextPaint textPaint;
            Typeface font;
            int textColor;
            float textSize = 42f, lineHeight = 57f, wordSpacing = 15f, lineSpacing = 15f;
            float onBirim, w, h;
            float leftPadding, rightPadding;
    
            public JustifiedTextView(Context context, String text) {
                    super(context);
                    this.text = text;
                    init();
            }
    
            private void init() {
                    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                    textColor = Color.BLACK;
            }
    
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
                    if (font != null) {
                            font = Typeface.createFromAsset(getContext().getAssets(), "font/Trykker-Regular.ttf");
                            textPaint.setTypeface(font);
                    }
                    textPaint.setColor(textColor);
    
                    int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
                    w = resolveSizeAndState(minw, widthMeasureSpec, 1);
                    h = MeasureSpec.getSize(widthMeasureSpec);
    
                    onBirim = 0.009259259f * w;
                    lineHeight = textSize + lineSpacing;
                    leftPadding = 3 * onBirim + getPaddingLeft();
                    rightPadding = 3 * onBirim + getPaddingRight();
    
                    textPaint.setTextSize(textSize);
    
                    wordSpacing = 15f;
                    Line lineBuffer = new Line();
                    this.linesCollection.clear();
                    String[] lines = text.split("\n");
                    for (String line : lines) {
                            String[] words = line.split(" ");
                            lineBuffer = new Line();
                            float lineWidth = leftPadding + rightPadding;
                            float totalWordWidth = 0;
                            for (String word : words) {
                                    float ww = textPaint.measureText(word) + wordSpacing;
                                    if (lineWidth + ww + (lineBuffer.getWords().size() * wordSpacing) > w) {// is
                                            lineBuffer.addWord(word);
                                            totalWordWidth += textPaint.measureText(word);
                                            lineBuffer.setSpacing((w - totalWordWidth - leftPadding - rightPadding) / (lineBuffer.getWords().size() - 1));
                                            this.linesCollection.add(lineBuffer);
                                            lineBuffer = new Line();
                                            totalWordWidth = 0;
                                            lineWidth = leftPadding + rightPadding;
                                    } else {
                                            lineBuffer.setSpacing(wordSpacing);
                                            lineBuffer.addWord(word);
                                            totalWordWidth += textPaint.measureText(word);
                                            lineWidth += ww;
                                    }
                            }
                            this.linesCollection.add(lineBuffer);
                    }
                    setMeasuredDimension((int) w, (int) ((this.linesCollection.size() + 1) * lineHeight + (10 * onBirim)));
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);
                    canvas.drawLine(0f, 10f, getMeasuredWidth(), 10f, textPaint);
                    float x, y = lineHeight + onBirim;
                    for (Line line : linesCollection) {
                            x = leftPadding;
                            for (String s : line.getWords()) {
                                    canvas.drawText(s, x, y, textPaint);
                                    x += textPaint.measureText(s) + line.spacing;
                            }
                            y += lineHeight;
                    }
            }
    
            public String getText() {
                    return text;
            }
    
            public void setText(String text) {
                    this.text = text;
            }
    
            public Typeface getFont() {
                    return font;
            }
    
            public void setFont(Typeface font) {
                    this.font = font;
            }
    
            public float getLineHeight() {
                    return lineHeight;
            }
    
            public void setLineHeight(float lineHeight) {
                    this.lineHeight = lineHeight;
            }
    
            public float getLeftPadding() {
                    return leftPadding;
            }
    
            public void setLeftPadding(float leftPadding) {
                    this.leftPadding = leftPadding;
            }
    
            public float getRightPadding() {
                    return rightPadding;
            }
    
            public void setRightPadding(float rightPadding) {
                    this.rightPadding = rightPadding;
            }
    
            public void setWordSpacing(float wordSpacing) {
                    this.wordSpacing = wordSpacing;
            }
    
            public float getWordSpacing() {
                    return wordSpacing;
            }
    
            public float getLineSpacing() {
                    return lineSpacing;
            }
    
            public void setLineSpacing(float lineSpacing) {
                    this.lineSpacing = lineSpacing;
            }
    
            class Line {
                    ArrayList<String> words = new ArrayList<String>();
                    float spacing = 15f;
    
                    public Line() {
                    }
    
                    public Line(ArrayList<String> words, float spacing) {
                            this.words = words;
                            this.spacing = spacing;
                    }
    
                    public void setSpacing(float spacing) {
                            this.spacing = spacing;
                    }
    
                    public float getSpacing() {
                            return spacing;
                    }
    
                    public void addWord(String s) {
                            words.add(s);
                    }
    
                    public ArrayList<String> getWords() {
                            return words;
                    }
            }
    }
    

    Add above class to your src folder and use this sample code to add to your layout:

    JustifiedTextView jtv= new JustifiedTextView(getApplicationContext(), "Lorem ipsum dolor sit amet... ");
    LinearLayout place = (LinearLayout) findViewById(R.id.book_profile_content);
    place.addView(jtv);
    
    0 讨论(0)
  • 2020-11-22 04:14

    Android does not yet support full justification. We can use Webview and justify HTML instead of using textview. It works so fine. If you guys not clear, feel free to ask me :)

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

    TextView Content Justification: Its easy guys just use android:justificationMode="inter_word" within your TextView tag.

     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="92dp"
        android:text="@string/contents"
        android:layout_margin="20dp"
        android:justificationMode="inter_word"
         />
    
    0 讨论(0)
  • 2020-11-22 04:15

    To justify text in android I used WebView

        setContentView(R.layout.main);
    
        WebView view = new WebView(this);
        view.setVerticalScrollBarEnabled(false);
    
        ((LinearLayout)findViewById(R.id.inset_web_view)).addView(view);
    
        view.loadData(getString(R.string.hello), "text/html; charset=utf-8", "utf-8");
    

    and html.

    <string name="hello">
    <![CDATA[
    <html>
     <head></head>
     <body style="text-align:justify;color:gray;background-color:black;">
      Lorem ipsum dolor sit amet, consectetur 
      adipiscing elit. Nunc pellentesque, urna
      nec hendrerit pellentesque, risus massa
     </body>
    </html>
    ]]>
    </string>
    

    I can't yet upload images to prove it but "it works for me".

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

    UPDATED

    We have created a simple class for this. There are currently two methods to achieve what you are looking for. Both require NO WEBVIEW and SUPPORTS SPANNABLES.

    LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

    SUPPORTS: Android 2.0 to 5.X

    SETUP

    // Please visit Github for latest setup instructions.
    

    SCREENSHOT

    Comparison.png

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