使用Kotlin Android扩展程序在不同布局中引用具有相同ID的视图 [英] Referencing views with the same id in different layouts with kotlin android extensions

查看:504
本文介绍了使用Kotlin Android扩展程序在不同布局中引用具有相同ID的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android项目中,我有两种布局:num_info和num_info_pack.两者都具有ID为"circle"的视图.因此,我认为通过layout_name.circle引用这些视图将解决问题:

In my Android project I have two layouts: num_info and num_info_pack. Both have views with id "circle". So I thought referencing those views by layout_name.circle would solve the problem:

val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false)
    inetView.circle.setBackgroundResource(background)

但是圆圈用红色下划线表示:

But circle is underlined with red and it says:

重载分辨率含糊不清.所有这些功能都匹配.

Overload resolution ambiguity. All these functions match.

公共值View.circle:查看!定义于 kotlinx.android.synthetic.main.num_info_pack.view

public val View.circle: View! defined in kotlinx.android.synthetic.main.num_info_pack.view

公共val View.circle:RelativeLayout!定义于 kotlinx.android.synthetic.main.num_info_inet_plus_pack.view

public val View.circle: RelativeLayout! defined in kotlinx.android.synthetic.main.num_info_inet_plus_pack.view

如果我具体说的是inetView.circle,为什么对正在谈论的圈子感到困惑?

Why is it confused about which circle I'm talking about if I'm specifically saying inetView.circle?

推荐答案

此处的解决方案在导入中.您必须导入两个布局,例如

The solution here is in the imports. You must be importing two layouts like

import kotlinx.android.synthetic.main.num_info_pack

import kotlinx.android.synthetic.main.num_info_inet_plus_pack

删除其中之一,并保留其中一个要导入的适当布局文件.它应该可以正常工作.

Remove one of them and keep one with the appropriate layout file that you want to import. It should work fine.

这篇关于使用Kotlin Android扩展程序在不同布局中引用具有相同ID的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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