在布局 activity_main 中循环。科特林新手

While loop in layout activity_main. New to Kotlin

这是一个简单的想法。不确定我是否可以在 activity_main 中完成。在我的布局中,我想让它基于 X 创建 table 行。仍然需要实现 X。但现在我不确定是否可以完成。

  
  
     // Where I try to create a while loop to loop the table row based on x.
        var i = 0
        var x = 3
    
        while (i > x) {
        println(i)
    
        <TableRow
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            android:layout_weight="1"
            android:background="#ffffff">
          
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:text=" order " />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:text="Del Note "
                />
    
         
        </TableRow>

    
        i++
        }

    
    </TableLayout>

好的所以我现在知道我错了,这不是应该做的。能否请我指导一下可以完成此操作的方式。

XML 仅用于布局、动画、矢量图等资源。 Kotlin 不能直接实现成一个 XML 文件,这就是为什么每个 activity 都会生成一个 Kotlin 代码文件和一个对应的 XML 文件。

您似乎在尝试将同一内容显示 3 次。为此,您需要使用 Recycler View 并使用 Kotlin 来控制它的功能。您可以阅读 Google Developer Documentation 上的 Recycler View 是什么以及如何执行此操作。附带说明一下,在初学者级别,Recycler View 刚开始时可能会非常混乱。