Get android#39;s id resource from a string(从字符串中获取android的id资源)
问题描述
在我的 Kotlin 应用程序中,我有一些 ImageViews
(在 activity_main.xml
中):imageView_0
、imageView_1
、imageView_2
和 imageView_3
.
In my Kotlin app I have some ImageViews
(in activity_main.xml
): imageView_0
, imageView_1
, imageView_2
and imageView_3
.
如何在从 0 到 3 的循环中访问视图?这行不通:
How can I access the view in a loop from 0 to 3? This won't work:
val imageView: ImageView = findViewById<ImageView>("R.id.imageView_" + index) as ImageView
推荐答案
for(i in 1..3){
val id: int=getResources().getIdentifier("imageview_"+i, "id",
getPackageName())
imageview[i]=findViewById(id) as ImageView
}
如果你在xml
、imageview_1
、imageview_2
、imageview_3
这篇关于从字符串中获取android的id资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!