开发者

Custom attrs parameter used in styles.xml

I have a set of custom Android layout parameters defined in attrs.xml. Now I would like to use some tags in my styles.xml file.

At the moment I get this error:

error: Error: No resource found that matches the given name: attr 'custom:tag'

I have tried declaring custom XML namesp开发者_开发知识库ace as follows:

<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.my.project"
>

hoping, that the same logic used in every layout declaration can be applied here, but with no success.


The XML namespace mechanism is used to namespace tags and attributes. When you define a style like this:

<?xml version="1.0" encoding="utf-8"?>
<resources
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.my.project">

    <style name="my_style"> <item name="custom:tag">some_value</item> </style>

</resources>

you are trying to apply XML namespacing to an attribute value, which won't work. In this case, you should specify the package name directly, like this:

    <style name="my_style"> <item name="com.my.project:tag">some_value</item> </style>

Now Android will be able to resolve where the attribute is defined.


The accepted solution did not work for me, but it shed some light upon the situation.

The custom attributes are resolved and can be referenced in a global project's package name, like "com.ltst.project". Even if you have multiple modules (with the same base package name) the resources would be resolved in a project's package name.

So for me it was enough to just omit any prefixes for custom attributes in a style.

Custom attribute:

<declare-styleable name="SampleView">
    <attr name="sample_color" format="reference" />
</declare-styleable>

Style:

<style name="SampleStyle">
    <item name="sample_color">@color/sample_color</item>
</style>


You can use the link

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

and define the prefix for each tag as app

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜