如何检测 TextView 中的换行符
How to detect line breaks in TextView
在我的 Android 应用程序中,我创建了 8 个堆叠在一起的 TextView。现在我想将一些纯文本加载到那些 TextView-Lines 中。目前我的字符串有一个“;”作为指示换行符的分隔符,但是如果我自动检测换行符而不是使用硬编码的分号方法会更方便。
这是我目前的字符串:
myString = "" +
"This seems to be some sort of spaceship,;" +
"the designs on the walls appear to be of;" +
"earth origin. It looks very clean here.;"
在我的另一个 class 中,我将此字符串加载到 8 个 TextView 中,我已使用“;”将其加载到 ArrayList 中。作为分隔符。
public fun fillLines(myString: String) {
// How To Make Line Breaks Automatic??
for(i: Int in str until myString.split(";").size) {
if(i > textViewArray.size - 1) {
break
}
textViewArray[i].text = myString.split(";")[i]
textViewArray[i].alpha = 1.0f
}
}
有什么方法可以获得与上面所示相同的结果,但无需将分隔符硬编码为“;”而是以某种方式自动检测 TextView 内部发生的换行符,然后将其用作分隔符以推进所有 8 个 TextView "Lines".
我需要将 8 个 TextView 作为单个堆叠在彼此之上的原因 "text lines" 是因为我想使用一种动画技术。
您可以用 html 填充文本视图。下面的例子。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
tvDocument.setText(Html.fromHtml(bodyData,Html.FROM_HTML_MODE_LEGACY));
} else {
tvDocument.setText(Html.fromHtml(bodyData));
}
如果你的分隔符;可以调用方法 replaceAll(";", "<br>");
好的,我现在开始工作了:
首先,您必须为文本视图添加这些属性:
android:singleLine="true"
android:ellipsize="none"
那么你可以这样做:
public fun fillStorylines() {
val linecap = 46
var finalLine: String
var restChars = ""
val index = 9999
val text1: String = "" +
"This seems to be some sort of spaceship, " +
"the designs on the walls appear to be of " +
"earth origin. It looks very clean here. "
for(j: Int in 0..index) {
try {
finalLine = ""
val lines: List<String> = (restChars + text1.chunked(linecap)[j]).split(" ")
for (i: Int in 0 until lines.size - 1) {
finalLine += lines[i] + " "
}
textViewArray[j].text = finalLine
textViewArray[j].alpha = 1.0f
restChars = lines[lines.size - 1]
} catch (ex: Exception) {
break
}
}
}
如果有人知道解决此问题的更优雅的方法,请继续,感谢您的反馈:)
换行变得相当复杂,所以我的建议是让 TextView
执行测量和布局以确定换行。您可以拥有一个与其他视图具有相同样式的不可见 TextView
,并将其附加到布局,以便它具有与您的个人 TextView
实例相同的宽度。从那里添加一个布局更改侦听器,然后您可以从 TextView
Layout
:
中检索各个行
myTextView.text = // your text string here
myTextView.addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
(view as? TextView)?.layout?.let { layout ->
// Here you'll have the individual broken lines:
val lines = (0 until layout.lineCount).map {
layout.text.subSequence(layout.getLineStart(it), layout.getLineVisibleEnd(it)
}
}
}
也就是说,这附带一个警告,即您将失去 TextView
提供的连字符,因此您可能希望在您的情况下完全禁用连字符。
在我的 Android 应用程序中,我创建了 8 个堆叠在一起的 TextView。现在我想将一些纯文本加载到那些 TextView-Lines 中。目前我的字符串有一个“;”作为指示换行符的分隔符,但是如果我自动检测换行符而不是使用硬编码的分号方法会更方便。
这是我目前的字符串:
myString = "" +
"This seems to be some sort of spaceship,;" +
"the designs on the walls appear to be of;" +
"earth origin. It looks very clean here.;"
在我的另一个 class 中,我将此字符串加载到 8 个 TextView 中,我已使用“;”将其加载到 ArrayList 中。作为分隔符。
public fun fillLines(myString: String) {
// How To Make Line Breaks Automatic??
for(i: Int in str until myString.split(";").size) {
if(i > textViewArray.size - 1) {
break
}
textViewArray[i].text = myString.split(";")[i]
textViewArray[i].alpha = 1.0f
}
}
有什么方法可以获得与上面所示相同的结果,但无需将分隔符硬编码为“;”而是以某种方式自动检测 TextView 内部发生的换行符,然后将其用作分隔符以推进所有 8 个 TextView "Lines".
我需要将 8 个 TextView 作为单个堆叠在彼此之上的原因 "text lines" 是因为我想使用一种动画技术。
您可以用 html 填充文本视图。下面的例子。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
tvDocument.setText(Html.fromHtml(bodyData,Html.FROM_HTML_MODE_LEGACY));
} else {
tvDocument.setText(Html.fromHtml(bodyData));
}
如果你的分隔符;可以调用方法 replaceAll(";", "<br>");
好的,我现在开始工作了:
首先,您必须为文本视图添加这些属性:
android:singleLine="true"
android:ellipsize="none"
那么你可以这样做:
public fun fillStorylines() {
val linecap = 46
var finalLine: String
var restChars = ""
val index = 9999
val text1: String = "" +
"This seems to be some sort of spaceship, " +
"the designs on the walls appear to be of " +
"earth origin. It looks very clean here. "
for(j: Int in 0..index) {
try {
finalLine = ""
val lines: List<String> = (restChars + text1.chunked(linecap)[j]).split(" ")
for (i: Int in 0 until lines.size - 1) {
finalLine += lines[i] + " "
}
textViewArray[j].text = finalLine
textViewArray[j].alpha = 1.0f
restChars = lines[lines.size - 1]
} catch (ex: Exception) {
break
}
}
}
如果有人知道解决此问题的更优雅的方法,请继续,感谢您的反馈:)
换行变得相当复杂,所以我的建议是让 TextView
执行测量和布局以确定换行。您可以拥有一个与其他视图具有相同样式的不可见 TextView
,并将其附加到布局,以便它具有与您的个人 TextView
实例相同的宽度。从那里添加一个布局更改侦听器,然后您可以从 TextView
Layout
:
myTextView.text = // your text string here
myTextView.addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
(view as? TextView)?.layout?.let { layout ->
// Here you'll have the individual broken lines:
val lines = (0 until layout.lineCount).map {
layout.text.subSequence(layout.getLineStart(it), layout.getLineVisibleEnd(it)
}
}
}
也就是说,这附带一个警告,即您将失去 TextView
提供的连字符,因此您可能希望在您的情况下完全禁用连字符。