android - 如何在 TextView 中禁用自动换行?
问题描述
我有一个TextView
包含 60 个字符的随机字符串。
该字符串是一个单词,不包含任何空格。
问题是在某些字符(例如@, &, %
)之后,我得到了一个我不想要的自动换行符。
我想要的结果是每一行都填满到最后,并且不存在随机换行符。
我试过设置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()
或类似的东西。
推荐阅读
- javascript - 为什么这个状态变量不是实时递增的?
- objective-c - 在 macOS 上以编程方式更新模式更改(暗模式、亮模式)设置的颜色(objective-c)
- reactjs - 具有两个样式化组件的 Div
- export - 不能在电脑上玩godot游戏作为软件
- arrays - 无法在 React 中将对象添加到数组
- python - HTTP 响应 403。是我的 Apache 配置问题还是我的 Python 对 URL 的 enoding 问题?
- flutter - Flutter: Navigator.pop(context);
- c# - C# IDisposable,MCSD 认证工具包中的一个令人困惑的问题(考试 70-483)
- alsa - ALSA 无法将 dmix 与路由一起使用
- python - 先找到不重复的字符