我的應用程式顯示一長行文本,如下所示:

我想在每一行添加一個“>”字符,所以我嘗試了以下代碼:
private String addPrefixChars(String text) {
StringBuilder builder = new StringBuilder();
StringTokenizer st = new StringTokenizer(text, NL);
builder.append(NL);
for (int i = 0, count = st.countTokens(); i < count; i ) {
builder.append(">").append(st.nextToken()).append(NL);
}
return builder.toString();
}
最后得到了這個:

請注意,整個文本只有一個“>”字符,而我試圖讓每個顯示的行都以“>”開頭。任何人有任何想法如何做到這一點?
uj5u.com熱心網友回復:
我試圖達到你所需要的。
文本 :
Lorem Ipsum 只是印刷和排版行業的虛擬文本。自 1500 年代以來,Lorem Ipsum 一直是業界標準的虛擬文本。當一個不知名的印刷商拿了一個型別的廚房,并把它打亂,制作了一本型別樣本書。它不僅經歷了五個世紀,而且經歷了電子排版的飛躍,基本保持不變。它在 1960 年代隨著包含 Lorem Ipsum 段落的 Letraset 表的發布而流行起來,最近還隨著 Aldus PageMaker 等桌面出版軟體(包括 Lorem Ipsum 的版本)而普及。
在每行中添加“>”的代碼TextView:
TextView content = findViewById(R.id.content);
content.post(() -> {
ArrayList<String> lines = new ArrayList<>();
String textContent = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. When an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
Layout layout = content.getLayout();
int lineCount = content.getLineCount();
for (int i = 0; i < lineCount; i ) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
if (lineStart <= textContent.length() && lineEnd <= textContent.length()) {
String lineString = textContent.substring(lineStart, lineEnd);
lines.add("> " lineString "\n");
}
}
content.setText("");
for (String line : lines)
content.append(line);
});
輸出 :

uj5u.com熱心網友回復:
我最終將包含原始文本的欄位更改為 TextView 并為回復創建了一個 EditText。我不必在原文前面加上“>”;相反,我只是將其斜體以將其與回復區分開來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473928.html
