简体   繁体   English

如何将库项目中的可绘制对象引用到 android 中的主项目布局中?

[英]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.我有一个 android 库项目和可重复使用的可绘制资源。
Lets say package name is: com.vijay.mylib;可以说 package 名称是: 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 .可以说它的 package 名称是: 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.在我的主要项目中,我有一个名为 main.xml 的布局。 I want to use some drawable rom library project in main.xml.我想在 main.xml 中使用一些可绘制的 rom 库项目。 How can i do it in xml?我该如何在 xml 中做到这一点?
Refering in normal way like "@drawable/myImage" didn't work for me.像“@drawable/myImage”这样以正常方式引用对我不起作用。 The referece chooser windows shows me the drawables only from main project.参考选择器 windows 仅向我显示主项目中的可绘制对象。 Not from library project.不是来自图书馆项目。

(Note: Both projects have different package for R.java. Does it has do anything with this?) (注意:两个项目对于 R.java 有不同的 package。它有什么用吗?)

You probably would need to add your own uri for library and use it to refer to drawable.您可能需要为库添加自己的 uri 并使用它来引用 drawable。

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

Then you should be able to access drawable using然后你应该能够使用drawable访问

@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:确定在库项目(lib 项目)和应用程序项目(app 项目)之间使用哪些可绘制资源的最佳方法是设计:

  1. By default, an app project's drawable resource will supersede the use of an identically named lib project's drawable resource.默认情况下,应用项目的可绘制资源将取代同名 lib 项目的可绘制资源的使用。 Therefore, the best way to ensure the lib project resource is used, is to remove the resource from the app project altogether.因此,确保使用 lib 项目资源的最佳方法是从 app 项目中完全删除该资源。

  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!即,如果您的 lib 项目资源位于drawable-mdpi中,请确保该文件夹在应用项目中也称为drawable-mdpi

In 2022, it's easy to use, if the lib is imported to your project, as it's shown in below picture.在 2022 中,它很容易使用,如果将 lib 导入到您的项目中,如下图所示。

1- where do you want use it, just write R.drawable.any_png 1-你想在哪里使用它,只需写R.drawable.any_png

2- open icon in left side, then chose the libraries item 2-在左侧打开图标,然后选择libraries项目

3- you can see all the pictures are in two place in 3,2 3-您可以看到所有图片都在 3,2 的两个位置在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Eclipse:如何从另一个项目引用库项目的NDK库? - Android Eclipse: How to reference library project's NDK library from another project? Android:可从库项目绘制到应用程序 - Android: Drawable from library project to app 从Android中的库项目访问可绘制的资源 - Accessing drawable rescources from a library project in Android 如何将意图数据从主项目发送到android中的库项目? - How to send intent data from main project to library project in android? Android:如何从Eclipse ADT应用程序项目引用库项目? - Android: How to reference a library project from an Eclipse ADT application project? 如何从Android的库项目中调用驻留在应用程序项目中的drawable? - How to call a drawable which resides in app project from library project in android? 从另一个Android库项目引用Android库项目 - Reference Android Library project from another android library project Android:如何从库回调到主应用程序项目? - Android: How to callback from a library to the main app project? Android-项目默认使用appCompat库项目的资源进行布局 - Android - project defaulting to appCompat library project's resources for layout 如何在Android中使用库项目中的布局文件 - How to use Layout file from library project in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM