首页 > 解决方案 > 如何在 kotlin 中创建视图并将 TextView 放入其中

问题描述

我想通过代码在 kotlin 中的 View 对象中添加 TextView

我确实创建了视图,但我无法在其中添加文本视图

val view1 : View
  view1 = View(this)
  myLayout.addView(view1)
  view1.layoutParams.height = (Height * 0.35).toInt()
  view1.layoutParams.width = (Width * 0.8).toInt()
  view1.x = (1+ Width*0.10).toFloat()
  view1.y = (Height*0.15).toFloat()
  view1.setBackgroundColor(Color.rgb(128,0,128))

标签: androidkotlin

解决方案


val view1 : TextView = TextView(this)
myLayout.addView(view1)
view1.layoutParams.height = (Height * 0.35).toInt()
view1.layoutParams.width = (Width * 0.8).toInt()
view1.x = (1+ Width*0.10).toFloat()
view1.y = (Height*0.15).toFloat()
view1.text = "Some text"
view1.setBackgroundColor(Color.rgb(128,0,128))

推荐阅读