在我的Kotlin应用程序中,我有一些ImageViews(位于activity_main.xml中):imageView_0,imageView_1,imageView_2和imageView_3.
如何从0到3循环访问视图?这行不通:
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