How to put new placeholder resources into Android Studio project (“tools:sample” resources)?

前端 未结 4 1187
青春惊慌失措
青春惊慌失措 2020-12-13 01:09

Background

Sometimes, you wish to put some placeholders to be shown only on the IDE, in layout files.

As an example, you can use this:

<         


        
4条回答
  •  萌比男神i
    2020-12-13 01:55

    I guess you have got the answer to other questions precisely by people who answered before me, but I can give you a workaround solution for particularly this question

    Is it possible to add more images to be used this way?

    I have faced this problem for quite a bit of time and found a workaround for it.

    My issue was that I couldn't use a placeHolderImage in a SimpleDraweeView which is a part of Fresco library of Facebook, if you didn't knew. But since an ImageView is not much complex like a SimpleDraweeView, and as the workaround worked with SimpleDraweeView, even though I haven't tried it with an ImageView, it might surely work with it too.

    Here is the solution.

    You just have to create a json file inside the sampledata folder, but this time, you have to do it this way.

        {
          "data": [
            {
              "image": "@drawable/ic_jam_jar"
            },
            {
              "image": "@drawable/ic_rice_bag"
            },
            {
              "image": "@drawable/store_avatar"
            },
            {
              "image": "@drawable/exclamation_icon"
            }
          ]
       }
    

    As you can see, I have used the reference for drawable resource in place of value for image key. And it works in my case. What you get here in a way is an array of image resources which you can use inside the item of a RecyclerView to be previewed in Design View in Android Studio.

    My RecyclerView code is as follows

    
    

    Remember not to skip any of the extra tags which I used, as it might not produce the required output in preview if you miss any. You can customize the itemCount attribute value according to your requirement of number of images you are using. Also, orientation might be vertical or horizontal according to your use-case.

    You can simply use tools:src="@sample/your_json_file.json/data/image inside your recyclerview item xml file's ImageView to load sample images there.

    Try it and let me know if this works in your case or not.

提交回复
热议问题