如何在values / ids.xml中获得自定义ID的引用
我在回收视图适配器中使用anko来创建视图的视图。 我已经做到了这一点,但不知道如何使用kotlin综合视图id(我想得到它没有findViewById)
值/ ids.xml
我的Anko getView代码:
private fun getView(context: Context): View{ return with(context){ linearLayout { lparams(width = matchParent, height = wrapContent) padding = dip(10) orientation = android.widget.LinearLayout.HORIZONTAL //Task Number textView { id = R.id.txv1 text = "TextView 22" textSize = 16f typeface = Typeface.MONOSPACE padding =dip(5) }.lparams(){ weight = 1f } //Task Name textView { id = R.id.txv2 text= "TextView 33" textSize = 16f typeface = android.graphics.Typeface.DEFAULT_BOLD padding =dip(5) } } } }
我从ids.xml分配自定义ID,但如何得到它没有findViewById
谢谢
经过很多的研究,我得出的结论是,由anko的id创建意见的直接引用现在是不可能的。
解决方法是使用 –
val txv1 = findViewById(R.id.txv1) as TextView
要么
声明一个variables来保存在anko方法内创建的视图的引用。
代码如下 –
var txv1: TextView? = null private fun getView(context: Context): View{ return with(context){ linearLayout { txv1 = textView { text = "TextView" } } } }
希望这会帮助别人。 谢谢