Referencing views with the same id in different layouts with kotlin android extensions

社会主义新天地 提交于 2019-12-05 03:49:19

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

import kotlinx.android.synthetic.main.num_info_pack

and

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.

In addition to the already very good answers, if you have the same IDs in multiple layouts in your project, it shouldn't matter which one you pick. Similar IDs, regardless of which layout it is defined, end up pointing to the same view. So, you can discard the other imports, leaving only the layout(s) that matters to you in the current activity/fragment/view

Hope that helps

glee8e

I don't have android studio in hand now but I think this will solve your problem:

package XXX

import kotlinx.android.synthetic.main.num_info_inet_plus_pack.view.circle as inetViewCircle
import kotlinx.android.synthetic.main.num_info_pack.view as circle
//...
val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false)
inetViewCircle.setBackgroundResource(background)

Don't know if this will works because I can't test it right now. Please let me know whether it's working.

The problem is a name clash, so I think import alias may help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!