【发布时间】:2022-11-27 02:10:59
【问题描述】:
我正在尝试使用 Java 在 Android Studio 中创建一个填补空白的游戏,为此我正在做一个句子,将关键字(由用户填充)与字符串分开,然后在水平 LinearLayout 中添加如下字符串(在垂直布局内):
TextView 关键字前 + TextView 关键字 + TextView 关键字后
在下面的不同 LinearLayout 中,我有一个以下 TextView (TextView Line3) 制作第二行,其宽度与上面的水平 LinearLayout 相同。 -- 类似于第 2 行
由于 TextView after keyword 太长并且在“TextView keyword”之后开始第二行,我想将“TextView after keyword”的第二行内容移至“TextView Line3”。
问题是它一直说只有 1 行,而“关键字后的 TextView”显示两行
我这样定义它们:
private TextView firstSentence, secondSentence, thirdSentence;
public TextView answerText;
private String sentence = "I do not like anyone in this world of idiots";
private boolean newLineBoolean = true;
private String keyword = "like";
private String[] sentenceDivision;
private String displayForKeyword = "";
private String thirdLine = "";
这个在onCreate
answerText = findViewById(R.id.answerPlace);
firstSentence = findViewById(R.id.firstSentence);
secondSentence = findViewById(R.id.secondSentence);
thirdSentence = findViewById(R.id.thirdSentence);
sentenceDivision = sentence.split(keyword);
firstSentence.setText(sentenceDivision[0]);
secondSentence.setText(sentenceDivision[1]);
for(int i = 0; i<keyword.length();i++)
{
displayForKeyword = displayForKeyword + " ";
}
answerText.setText(displayForKeyword);
checkNumberOfLines();
而这个方法
private void checkNumberOfLines(){
String firstWords = sentenceDivision[1].substring(0, sentenceDivision[1].lastIndexOf(" "));
String lastWord = sentenceDivision[1].substring(sentenceDivision[1].lastIndexOf(" ") + 1);
sentenceDivision[1] = firstWords;
thirdLine = lastWord + " " + thirdLine;
secondSentence.setText(sentenceDivision[1]);
thirdSentence.setText(thirdLine);
secondSentence.post(new Runnable() {
@Override
public void run() {
int lineCount = secondSentence.getLineCount();
if (lineCount > 0) {
checkNumberOfLines();
}
else{ newLineBoolean = false;
}
}
});
}
但是显示如下:
有人知道为什么吗?提前致谢!
【问题讨论】:
标签: java android textview line-count