动态添加View
效果图:
具体代码:
//1.寻找到可以添加控件的一些根布局:如LinearLayout
val linearLayout = findViewById<View>(R.id.line_view) as LinearLayout
val lv0_params = LinearLayout.LayoutParams(dp2px(this, 200f), dp2px(this, 50f))
for (index in 1..10){
//根据业务需求,加载单个UI的布局
val convertView = View.inflate(this, R.layout.list_item, null)
val tv_one = convertView.findViewById<TextView>(R.id.wenben_tv_one)
val item_line = convertView.findViewById<View>(R.id.item_line)
val lin_item = convertView.findViewById<RelativeLayout>(R.id.lin_item)
tv_one.text = "更改的文本:"+index
if (index == 10){
item_line.visibility = View.GONE
}
linearLayout.addView(convertView, lv0_params)//要添加lv0_params,否则宽高不准
lin_item.setOnClickListener {
Toast.makeText(this,"点击文本:"+index,Toast.LENGTH_SHORT).show()
}
}