我有一个包含 60 个字符的随机字符串的 TextView
。
该字符串是单个单词,不包含任何空格。
问题是在某些字符(例如 @, &, %
)之后我得到一个我不想要的自动换行符。
我想要的结果是每一行都被填满,并且没有随机的换行符。
我已经尝试设置 breakStrategy
并更新 hyphenationFrequency
,但这没有帮助。
如何防止这种情况发生?
更新:感谢@Darkman,这是我编写的解决方案。它检查在没有换行符的情况下一行可以容纳多少个字符,并在末尾附加 \n
。
请注意,对于我的用例,字符串没有空格,并且我使用的是等宽字体。
fun TextView.toNonBreakingString(text: String?): String {
if (text == null) return ""
val container = parent as? ViewGroup ?: return text
val lineWidth = (container.width - container.paddingStart - container.paddingEnd).toFloat()
val maxCharsInOneLine = paint.breakText(text, 0, text.length, true, lineWidth, null)
if (maxCharsInOneLine == 0) return text
val sb = StringBuilder()
var currentLine = 1
var end = 0
for (i in 0..text.count() step maxCharsInOneLine) {
end = currentLine * maxCharsInOneLine
if (end > text.length) end = text.length
sb.append(text.subSequence(i, end))
sb.append("\n")
currentLine = currentLine.inc()
}
if (end < text.length) {
val remainingChars = text.length - end
sb.append(text.takeLast(remainingChars))
}
return sb.toString()
}
最佳答案
实现所需结果的一种方法是使用 monospace
字体、左重力和一点点编程。
<TextView
android:layout_height="wrap_content"
android:layout_width="200dp"
android:text="akAOCYMKIpVmSwhHtWS%fBFK7eWi%19a590344gZqGQtkTcRv^1lFqH#F@Lhr"
android:gravity="left"
android:background="#708BCD"
android:id="@+id/tv"
android:textSize="20sp"
android:typeface="monospace"/>
//Note :: Auto-updated
public final void wrapText(final TextView textView, final String text)
{
final float textSize = (textView.getTextSize() / 250F) * 150F;
final int textLen = text.length();
final int columns = (int)(textView.getWidth() / textSize);
final int lines = (int)((float) textLen / columns);
final StringBuilder sb = new StringBuilder(textLen + lines);
for(int i=0, n=0; i < textLen; i+=columns, ++n) {
sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
if(n<lines) sb.append("\n");
}
textView.setText(sb.toString());
}
或者
//Note :: Auto-updated
public final void wrapText(final TextView textView)
{
final float textSize = (textView.getTextSize() / 250F) * 150F;
final CharSequence text = textView.getText();
final int textLen = text.length();
final int columns = (int)(textView.getWidth() / textSize);
final int lines = (int)((float) textLen / columns);
final StringBuilder sb = new StringBuilder(textLen + lines);
for(int i=0, n=0; i < textLen; i+=columns, ++n) {
sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
if(n<lines) sb.append("\n");
}
textView.setText(sb.toString());
}
或者
// Note :: Self-update
public final String wrapText(final TextView textView, final String text)
{
final float textSize = (textView.getTextSize() / 250F) * 150F;
final int textLen = text.length();
final int columns = (int)(textView.getWidth() / textSize);
final int lines = (int)((float) textLen / columns);
final StringBuilder sb = new StringBuilder(textLen + lines);
for(int i=0, n=0; i < textLen; i+=columns, ++n) {
sb.append(text.subSequence(i, Math.min(textLen, i + columns)));
if(n<lines) sb.append("\n");
}
return sb.toString();
}
// Implementation:
// textView.setText(wrapText(textView, "akAOCYMKIpVmSwhHtWS%fBFK7eWi%19a590344gZqGQtkTcRv^1lFqH#F@Lhr"));
并且您需要在测量布局后调用其中一种方法。这意味着在 onCreate()
、onCreateView()
或类似的东西之外。
关于android - 如何禁用 TextView 中的自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67919361/