Android textview断行行为(如何拆分最后一个单词)

问题描述 投票:6回答:5

我正在写一个在屏幕上弹出文本的应用程序,通常它长于1行。如果一行的最后一个单词太长而不适合它,textview就会把它放在下一行的开头。有没有办法修改这种行为?当最后一个字很长时,结果很糟糕。

基本上我想要实现这样的目标

| Lorem存有胡萝卜,consecte | |图尔本科生研究。 Lorem存有DOL | |或者胡萝卜,提高本科| |开发者。 |

而不是我现在得到的。

| Lorem存有胡萝卜,| |本科番茄汤。 LOREM | |非常胡萝卜,西红柿| |本科生研究。 |

谢谢!

附加信息

我有这个textview显示有不同长度的随机引号。我需要的是一种分割每一行的最后一个词的方法。

我正在寻找一种像this这样的方法!

这是布局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" />

android textview line-breaks
5个回答
2
投票

我决定切换到WebView,因为使用HTML我可以访问理由。你可以读一下herehere,如果你想注射css this is a good tutorial. 如果你正在寻找肥胖,也许你可以看看Google's hypenator.js

所以,在这里回答自己是解决方案!

<?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" />

并在活动中

    ...
    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
投票

试试这个

 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;
}

0
投票
// 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"/>

0
投票

你可以以编程方式做到这一点。做就是了:

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

-2
投票

使用android:maxLine =行数

© www.soinside.com 2019 - 2024. All rights reserved.