简体   繁体   中英

How to reference a drawable from a library project into a main project's layout in android?

I have a android library project and reusable drawable resources in it.
Lets say package name is: com.vijay.mylib;

Then i have a main project and it uses the above library project.
Lets say its package name is: com.vijay.myproject .

Consider that I have linked library project with my main project correctly.

In my main project i have a layout called main.xml. I want to use some drawable rom library project in main.xml. How can i do it in xml?
Refering in normal way like "@drawable/myImage" didn't work for me. The referece chooser windows shows me the drawables only from main project. Not from library project.

(Note: Both projects have different package for R.java. Does it has do anything with this?)

You probably would need to add your own uri for library and use it to refer to drawable.

xmlns:vijay="http://schemas.android.com/apk/res/com.vijay.mylib"

Then you should be able to access drawable using

@vijay:drawable/myImage

The best way to determine which drawable resources are used between a library project (lib project) and an application project (app project) is down to design:

  1. By default, an app project's drawable resource will supersede the use of an identically named lib project's drawable resource. Therefore, the best way to ensure the lib project resource is used, is to remove the resource from the app project altogether.

  2. Another issue i came across is that if you want the app project resource to be used in preference, be sure that the drawable folder name in the app project is identical to the resource folder name in the library project. Ie if your lib project resources live in drawable-mdpi , ensure the folder is called drawable-mdpi in the app project too!

In 2022, it's easy to use, if the lib is imported to your project, as it's shown in below picture.

1- where do you want use it, just write R.drawable.any_png

2- open icon in left side, then chose the libraries item

3- you can see all the pictures are in two place in 3,2 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM