如何制作Android自组件并从jar库中使用它?

发布于 2024-11-05 13:40:12 字数 1313 浏览 7 评论 0原文

我正在尝试创建一个 android 组件,可以将其作为 jar 库轻松添加到 android 项目中。

为此,我必须在“res/values/attr.xml”中创建新的 xml 属性,并使用以下路径将其添加到我的图形 xml 元素中:

 xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"

然后,我将此项目作为 jar 库导入到另一个项目中。 要在新项目中创建图形组件,我必须更改以下路径:

 xmlns:app="http://schemas.android.com/apk/res/com.mylibrary"

但路径不正确:找不到自定义属性。

我设法将 R 文件集成到库 jar 中,我可以从 xml 访问它来声明一个自定义组件,如下所示:

<PreferenceScreen
  xmlns:android = "http://schemas.android.com/apk/res/android"
 xmlns:app = "http://schemas.android.com/apk/res/com.myLibraryPackage">

<com.myLibraryPackage.mySelfComponent
     android:title="Name"
     android:key="name"
     app:hintText="Input your name"
     android:dialogTitle="Your name "
     app:validator="com.myLibraryPackage.myValidatorClass" />

奇怪的是,如果我将文件 attr.xml 放入项目的资源中,它就会起作用,这意味着它找到 com.myLibraryPackage.mySelfComponent。在这种情况下,为什么它也找不到 com.myLibraryPackage ?

(我也尝试替换

xmlns:app="http://schemas.android.com/apk/res/com.myLibraryPackage"

xmlns:app="http://schemas.android.com/apk/res/com.myApplicationPackage"

,但它仍然不起作用)

我更喜欢使用 jar 来促进其在项目中的集成!

有谁遇到过这样的问题可以帮助我吗?

谢谢。

I'm trying to create an android component that can be easily added to android projects as a jar library.

For this, I had to create new xml attributes in "res/values/attr.xml" that I add to my graphic xml element using the path:

 xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"

Then I import this project as a jar library into another project.
To create my graphic components in the new project, I must change the path below:

 xmlns:app="http://schemas.android.com/apk/res/com.mylibrary"

But the path is incorrect: the custom attributes are not found.

I managed to integrate the R file in the library jar and I could access it from my xml to declare a custom component like this:

<PreferenceScreen
  xmlns:android = "http://schemas.android.com/apk/res/android"
 xmlns:app = "http://schemas.android.com/apk/res/com.myLibraryPackage">

<com.myLibraryPackage.mySelfComponent
     android:title="Name"
     android:key="name"
     app:hintText="Input your name"
     android:dialogTitle="Your name "
     app:validator="com.myLibraryPackage.myValidatorClass" />

What is strange is that if I put my file attr.xml in resources of my project, it works, which means it find com.myLibraryPackage.mySelfComponent. In that case, why it can't find also com.myLibraryPackage ?

(I also tried replacing

xmlns:app="http://schemas.android.com/apk/res/com.myLibraryPackage"

by

xmlns:app="http://schemas.android.com/apk/res/com.myApplicationPackage"

but it still doesn't work)

I would have preferred to use a jar to facilitate its integration in a project !

Has anyone encountered a problem like this who could help me?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-11-12 13:40:12

我正在尝试创建一个android
可以轻松添加的组件
android 项目作为 jar 库。

如果您想重用代码和资源,则无法使用 jar 文件来实现。您需要将库转换为库项目

然后我将此项目作为 jar 导入
库到另一个项目。到
在中创建我的图形组件
新项目,我必须更改路径
如下:

如果您使用的是库项目,您仍然需要引用自定义属性,就好像它包含在应用程序中一样(因为 android 会在编译应用程序时将所有资源合并在一起):

xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"

I'm trying to create an android
component that can be easily added to
android projects as a jar library.

If you want reuse code and resources, it won't be possible to do it with a jar file. You'll need to convert your library to a library project.

Then I import this project as a jar
library into another project. To
create my graphic components in the
new project, I must change the path
below:

If you're using a library project, you'd still reference the custom attribute as if it were contained in the application (since android will merge all the resources together when the application is compiled):

xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文