我的res xml具有linearlayout和一个按钮
<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" />
单击按钮将一些char数组一一添加到linearLayout中]
val chars = "Hello".toCharArray() btn_add_text.setOnClickListener { linearLayout.removeAllViews() chars.forEachIndexed { index, char -> val tv = Textview(this) tv.textSize = 36f tv.text = char tv.id = index linearLayout.addView(tv) linearLayout.invalidate() }
forEachIndexed循环完成后,线性布局刷新,可以看到[H] [e] [l] [l] [o]五个文本视图。但是我想在每个linearLayout.addView(tv)之后刷新linearLayout。
我的res xml具有linearlayout和一个按钮
据我所知,如果您想重绘视图,则调用无效,并且如果要更新视域,也需要调用requestLayout。
我认为linearlayout的刷新速度如此之快,您看不到中间的刷新,您可以做的是,使用工作线程并使其在每次迭代之间休眠500 ms,然后通过处理程序将数据发布到主线程,每次更改角色都将可见。