简体   繁体   中英

How create View and Putting TextView inside it in kotlin

I want add TextView inside View object in kotlin by code

I did create View but I can't add Text View in inside it

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

Your parent view must be ViewGroup. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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