使用Kotlin在android中动态创建控件 [英] Dynamically create controls in android using Kotlin

查看:1567
本文介绍了使用Kotlin在android中动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java来动态创建控件,我们使用类似 TextView textview=new TextView(getApplicationContext());

Using java, to create a control dynamically we use something like TextView textview=new TextView(getApplicationContext());

在Kotlin中如何做同样的事情? var textview = TextView 不起作用,var textview as TextView

how can the same be done in Kotlin? var textview = TextView does't work, nor does var textview as TextView

不幸的是,甚至还没有遇到任何适用于Android的好的kotlin教程.

unfortunately, haven't even encountered any good kotlin tutorials for android.

更新-实际上是在尝试使用自定义布局创建动态列表视图.

update-Actually am trying to create dynamic listview with a custom layout.

推荐答案

您可以通过调用TextView的构造函数,如下所示:

You can, by calling the constructor of TextView, like so:

var textview = TextView(this) // "this" being the Activity

请参阅官方文档中的创建实例.

See creating instances in the official documentation.

这篇关于使用Kotlin在android中动态创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆