Android的库项目采用申报,设置样式 - 如何编写? [英] Android library project uses declare-styleable -- how to compile?

查看:143
本文介绍了Android的库项目采用申报,设置样式 - 如何编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义preferences控制,我已经在价值观/ attrs.xml定义了几个属性。只是集中谈话,这里的属性,可以在值/ attrs.xml找到一个例子:

I have a custom preferences control that I have defined a few attributes for in values/attrs.xml. Just to focus the conversation, here is an example of attributes that could be found in values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="android:text"/>
        <attr name="android:textColor"/>            
        <attr name="extraInformation" format="string" />
    </declare-styleable>
</resources>

要使用的属性,你可以使用你想要使用它了xmlns标记,它看起来是这样的:

To use the attributes, you use an xmlns tag where you want to use it, and it looks something like this:

xmlns:custom="http://schemas.android.com/apk/res/com.conundrum.app.lib"

在这里存在一个问题:在XMLNS定义是指库的包名称,而这个资源编译好库中的项目。然而,Android项目,其中包括图书馆项目有不同的包名和Android试图合并所有的资源。当它得到这个的xmlns定义,它不太愿意,因为包名是在包括Android项目不同。

Herein lies the problem: the xmlns definition refers to the LIBRARY's package name, and this resource compiles fine in the LIBRARY project. However, the Android project that includes the Library project has a different package name and Android tries to merge all the resources. When it gets to this xmlns definition, it balks because the package name is different in the including Android project.

任何人有任何想法,利用图书馆项目仍然在包括Android项目有效的xmlns参考?

Anybody got any ideas for using xmlns references in Library projects that are still valid in including Android projects?

被宣告-styleables只是一个监督的Andr​​oid团队时,他们考虑的库?

Were declare-styleables just an oversight by the Android team when they considered libraries?

推荐答案

使用 http://schemas.android.com/apk/res-auto

我发现这个解决方案作为一个侧面说明,在 AndroidSVG 项目:

I found this solution given casually as a side-note in the AndroidSVG project:

另外:注意命名空间中的特殊架构的URL。如果您使用您自己的项目自定义视图,你将使用基于你的包名的URL。例如 http://schemas.android.com/apk/res/com.example .customview 。但由于自定义视图是一个包含库,你应该使用RES-AUTO的捷径。当你的应用程序是建立在真正的模式URL将被自动插入。

Aside: Note the special schema URL in the namespace. If you were using a custom View from your own project you would use a URL based on your package name. For example http://schemas.android.com/apk/res/com.example.customview. But since the custom View is in an included library you should use the res-auto short-cut. The real schema URL will be automatically inserted when your app is built.

(我固定一个错字 - 缺少/ RES /子目录 - 在上面的引用文本)

(I fixed a typo - missing /res/ subfolder - in the quoted text above)

这篇关于Android的库项目采用申报,设置样式 - 如何编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆