从字符串中获取android的id资源

all*_*nth 1 android imageview android-layout kotlin

在我的科特林应用程序我有一些ImageViews(中activity_main.xml): ,,imageView_0 和.imageView_1imageView_2imageView_3

如何在0到3的循环中访问视图?这不起作用:

val imageView: ImageView = findViewById<ImageView>("R.id.imageView_" + index) as ImageView
Run Code Online (Sandbox Code Playgroud)

Pet*_*dad 7

for(i in 1..3){
  val id: int=getResources().getIdentifier("imageview_"+i, "id", 
  getPackageName())
  imageview[i]=findViewById(id) as ImageView
}
Run Code Online (Sandbox Code Playgroud)

如果你在xml,imageview_1,imageview_2,imageview_3