簡體   English   中英

如何在Kotlin中創建View並將TextView放入其中

[英]How create View and Putting TextView inside it in kotlin

我想通過代碼在Kotlin的View對象中添加TextView

我確實創建了View,但是無法在其中添加Text View

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))
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))

您的父視圖必須是ViewGroup。 例如:

val parent: ViewGroup = LinearLayout(context)
    parent.addView(yourTextView)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM