Android, getting resource ID from string?

后端 未结 14 1375
忘掉有多难
忘掉有多难 2020-11-21 23:56

I need to pass a resource ID to a method in one of my classes. It needs to use both the id that the reference points to and also it needs the string. How should I best achie

14条回答
  •  故里飘歌
    2020-11-22 00:09

    In your res/layout/my_image_layout.xml

    
        
        
    
    

    To grab that ImageView by its @+id value, inside your java code do this:

    String row = "0";
    String column= "7";
    String tileID = "row_" + (row) + "_col_" + (column);
    ImageView image = (ImageView) activity.findViewById(activity.getResources()
                    .getIdentifier(tileID, "id", activity.getPackageName()));
    
    /*Bottom code changes that ImageView to a different image. "blank" (R.mipmap.blank) is the name of an image I have in my drawable folder. */
    image.setImageResource(R.mipmap.blank);  
    

提交回复
热议问题