【问题标题】:Separate second line of TextView into another TextView with Java使用 Java 将 TextView 的第二行分隔为另一个 TextView
【发布时间】: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;
            }
        }
    });

}

但是显示如下:

enter image description here

有人知道为什么吗?提前致谢!

【问题讨论】:

    标签: java android textview line-count


    【解决方案1】:

    可能是因为TextView.getLineCount()的定义

    public int getLineCount() {
        return mLayout != null ? mLayout.getLineCount() : 0;
    }
    

    如果 mLayout 是 BoringLayout,则 getLineCount() 始终返回 1。 要使用实际计算其行数的不同类型的文本布局 (DynamicLayout),您可以尝试通过调用 setTextIsSelectable 使文本可选择,或者在 TextView 中设置 Spannable 而不是 CharSequence(请参阅makeSingleLayout)。

    我同意你的句子,这不是你用例的最佳解决方案,通过使用 FlowLayout 而不是 LinearLayouts 并将每个单词放在单独的 TextView 中,你可能会减少麻烦。


    编辑以回答评论中的问题

    1. 将 FlowLayout 添加到活动的布局 xml 后,您可以为句子中的单词动态创建 TextView:
          // in onCreate()
          FlowLayout flowLayout = findViewById(R.id.flow_id);
      
          String[] words = sentence.split(" ");
          TextView wordText;
          for (int i = 0; i < words.length; i++) {
              String word = words[i];
              //if an edit text is needed for user input
              if (i == keywordIndex) {
                 wordText = new EditText(this);
                 wordText.setHint("____"); 
              } else {
                 wordText = new TextView(this);           
                 wordText.setText(word);
              }
              wordText.setTextColor(getResources()
                                         .getColor(R.color.your_color));
              wordText.setBackgroundColor(getResources()
                                         .getColor(android.R.color.white));
              flowLayout.add(wordText);
          }
      
      
      1. 是的,您可以使用带有 GridLayoutManager 或 StaggeredGridLayoutManager 的 RecyclerView 来布置句子单词的视图,但这需要创建 RecyclerView.Adapter。但我认为 RecyclerView 更适合显示一组垂直的 句子(例如 FlowLayouts)。

    【讨论】:

    • 哦,我不知道 FlowLayout - 在 Java 中仍然很新!我会试一试流式布局!我的句子只是一个例子,但理论上句子每次都会改变,我知道如何在 FlowLayout 中生成 TextViews 吗?你会为此推荐 RecyclerView 吗?多谢!
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2020-08-01
    • 2021-07-31
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多