如何在Kotlin中将TextView添加到View

时间:2018-11-30 15:58:51

标签: android kotlin

当我单击要显示(查看)的按钮时,我有按钮 我确实查看过,但是无法在其中添加textView

fun you_color_button_1(view: 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 name_label1 =TextView(this)
  myLayout.addView(name_label1)
  la.addView(name_label1)
  name_label1!!.layoutParams.height = (la.width * 0.20).toInt()
  name_label1!!.layoutParams.width = (la.height * 0.10).toInt()
  name_label1!!.x = (1+la.width * 0.75).toFloat()
  name_label1!!.y = (la.height * 0.15).toFloat()
  name_label1.text = "name"
  name_label1.textSize = (Width * 0.014).toFloat()
  name_label1.setTextColor(Color.WHITE)
  name_label1.setTypeface(null,Typeface.BOLD)
  name_label1.gravity = Gravity.CENTER

  }

this is what I want

我也想通过代码查看的拐角半径

1 个答案:

答案 0 :(得分:0)

当您单击下面提到的按钮时     https://i.stack.imgur.com/UgRzA.png 您应该在当前xml中根据需要添加该视图,并使该视图的可见性消失...然后,在单击事件上单击按钮时,根据需要验证数据,并直接使该视图的可见性变为可见。希望对您有所帮助。

最初查看的可见性->

yourView.visibility = View.GONE

btn.setOnClickListener{

     YourView.visibility = View.VISIBLE
}