Android textview break line behaviour (how to spli

2020-06-18 05:54发布

问题:

I'm writing an apply that pops texts on the screen and usually it is longer than 1 line. If the last word of a line is too long to fit in it, textview would just put it at the beginning of the next line. Is there a way to modify this behaviour? When the last word is a long one the results are horrible.

Basically I'd like to achieve something like this

|Lorem ipsum dolor sit amet, consecte|
|tur adipiscing elit. Lorem ipsum dol    |
|or sit amet, consectetur adipiscing     |
|elit.                                                     |

Instead of what I'm getting now.

|Lorem ipsum dolor sit amet,          |
|consectetur adipiscing elit. Lorem |
|ipsum dolor sit amet, consectetur |
| adipiscing elit.                             |

Thank you!

ADDITIONAL INFO

I have this textview that displays random quotes that have different lengths. What I need is a way to split the last word of every line.

I'm looking for a way to do something like this!

Here is the layout xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title" />

<TextView
    android:id="@+id/quote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/quote" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="refresh"
    android:text="@string/tellmemore" />

回答1:

I decided to switch to a WebView because with HTML I'd have access to justification. You can read about it here and here and if you want to inject css this is a good tutorial.
If you are looking for hypenation maybe you can look into the Google's hypenator.js

So, to answer to myself here is the solution!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFD0" >

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="refresh"
    android:text="@string/tellmemore" />

and in the activity

    ...
    WebView mWebView;
    @Override
    protected void onCreate(Bundle savedIstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedIstanceState);
        setContentView(R.layout.webview);
        mWebView = (WebView) findViewById(R.id.webview);
        load();}

        public void load() {
        // some other code to parse the string from a database
        String text = "<html> <head>" + tokens[1].toString()"</head><body>" + "<p align=\"justify\">"+ tokens[0].toString() + "</p> " + "</body></html>";       
        mWebView.loadData(text, "text/html", "utf-8");}


回答2:

Try this

 private String getWidthFitString(String input) {
    Paint paint = text.getPaint();
    // you can define max width by yourself
    int maxWidth = getContentMaxWidth();
    float width = paint.measureText(input);
    if (width > maxWidth) {
        List<String> words = Arrays.asList(input.split("\\s"));
        int breakLinePosition = 0;
        String toBreakLineText;
        List<String> toBreakLineWords = new ArrayList<>();
        while (breakLinePosition < words.size()) {
            toBreakLineWords.add(words.get(breakLinePosition));
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            float currentWidth = paint.measureText(toBreakLineText);
            if (currentWidth > maxWidth) {
                break;
            }
            breakLinePosition ++;
        }
        if (breakLinePosition > 1) {
            toBreakLineWords.remove(toBreakLineWords.size() - 1);
            toBreakLineText = TextUtils.join(" ", toBreakLineWords);
            List<String> fromBreakLineWords = new ArrayList<>();
            for (int i = breakLinePosition; i < words.size(); i++) {
                fromBreakLineWords.add(words.get(i));
            }
            return toBreakLineText + "\n" + getWidthFitString(TextUtils.join(" ", fromBreakLineWords));
        } else {
            return input;
        }
    }
    return input;
}


回答3:

You can do that programatically. Just do:

myString.replace(" ", "\u00A0");
myTextView.setText(myString);


回答4:

// trh this
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet,consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        android:gravity="clip_horizontal"/>


回答5:

use android:maxLine = number of lines