在添加查看一些文本视图后,如何使Android linearlayout刷新?

在添加查看一些文本视图后,如何使Android linearlayout刷新?,android,kotlin,android-linearlayout,Android,Kotlin,Android Linearlayout,我的res xml有一个线性布局和一个按钮 <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginStart="32dp" android:layout_marginTop="32dp"

我的res xml有一个线性布局和一个按钮

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal"

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_add_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="84dp"
        android:text="Button" />
ForAchineDexed循环完成linearLayout刷新后,可以看到[H][e][l][l][o]五个文本视图。
但是我想在每次linearLayout.addView(tv)之后刷新linearLayout。

我认为linearLayout刷新太快了,你无法看到中间刷新,你可以做的是,使用一个工作线程,在每次迭代之间让它休眠500毫秒,然后通过处理程序将数据发布到主线程,您对字符的每次更改都将可见。

据我所知,如果您想重新绘制视图,请调用invalidate;如果您想更新视图边界,还需要调用requestLayout。

如果您想一步一步地查看,可以尝试以下操作:

val handler = Handler()
btn_add_text.setOnClickListener {
linearLayout.removeAllViews()
chars.forEachIndexed { index, char ->
      val tv = TextView(context!!)
      tv.textSize = 24f
      tv.text = char.toString()
      tv.id = index
      handler.postDelayed(Runnable {
          linearLayout.addView(tv)
      },500 * index.toLong())
    }
}

谢谢你,卡斯姆·兹德米尔。它使我的布局跳舞。我必须学习Runnable!!谢谢你,安德烈·托德。我添加了调用invalidate和requestLayout。但在Android Studio调试模式下,它似乎不起作用。因为akashzincle的回答速度太快,或者布局刷新在整个循环结束后触发,我看不到这一点。也许主线程会阻止ui线程,直到循环完成。
val handler = Handler()
btn_add_text.setOnClickListener {
linearLayout.removeAllViews()
chars.forEachIndexed { index, char ->
      val tv = TextView(context!!)
      tv.textSize = 24f
      tv.text = char.toString()
      tv.id = index
      handler.postDelayed(Runnable {
          linearLayout.addView(tv)
      },500 * index.toLong())
    }
}