【问题标题】:How do i change dynamically textview in one single row only如何仅在一行中动态更改文本视图
【发布时间】:2018-04-28 14:31:10
【问题描述】:

正如您在this 图像中看到的,我的坐标正在动态变化\n。我希望它只显示一行中的最后一个坐标,只是动态变化,我该怎么做?

这是我的代码:

tv_loc.append("Lattitude: " + lattitude + "  Longitude: " + longitude + "\n");

【问题讨论】:

  • 我尝试在 android:maxLines="1" 中设置,它只停留在第一个坐标中
  • 如果只想要一行,则将tv_loc.append()改为tv_loc.setText()
  • @Sagar 是的,它有效,谢谢
  • 太棒了!我已添加为答案,请帮我批准,以便其他人也能受益。

标签: android android-layout textview


【解决方案1】:

如果你使用append(),那么每次你调用它时,它都会追加一个新行。由于您的意图是更新同一行,因此请使用以下

tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");

代替

tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");

【讨论】:

    【解决方案2】:

    您需要更新文本而不是附加它。

    当您附加文本时,它将添加到字符串的末尾。假设您有以下代码:

    String tv_loc = "";
    
    For(int i = 0; i < 10; i++) {
        tv_loc.append(i + " ");
    }
    
    system.out.println(tv_loc);
    

    它将打印0 1 2 3 4 5 6 7 8 9,因为您正在附加它。

    要解决这个问题,您需要更新文本,为此您可以使用setText() 函数(假设tv_loc 是一个TextView 对象),例如

    tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude);

    \n 不是必须的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2012-01-24
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多