简体   繁体   English

如何以编程方式在 Android Studio 中设置 TextView 的 x 和 y 值

[英]How to set x and y values of TextView in Android Studio Programmatically

I am trying to set a random value for textview of x and y together in 1 forloop.我正在尝试在 1 个 forloop 中为 x 和 y 的 textview 设置一个随机值。

I have a function我有一个 function

fun createTextView(text: String, index: Int): TextView

and on another function, I am using a for loop to create the TextView based on number of words in a list.在另一个 function 上,我使用 for 循环根据列表中的字数创建 TextView。

       val words = arrayListOf("abc","def","ghi")

       for ((wordIndex, i) in words.withIndex()){
            createTextView(i,wordIndex).x = some random number
        }

From this loop, I can only set either x or y value for my textview.在这个循环中,我只能为我的 textview 设置 x 或 y 值。 How do I make it such that I can set both x and y value inside my for loop?如何使我可以在我的 for 循环中同时设置 x 和 y 值?

You can just store the return value in a variable and do it like this.您可以将返回值存储在一个变量中,然后这样做。

   val words = arrayListOf("abc","def","ghi")

   for ((wordIndex, i) in words.withIndex()){
      val textView = createTextView(i, wordIndex)
      textView.x = some random number
      textView.y = some random number
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM