Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom attrs parameter used in styles.xml

Tags:

android

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 namespace 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.

like image 272
LambergaR Avatar asked Nov 24 '10 10:11

LambergaR


People also ask

What is Attrs XML?

An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.

What is style XML file?

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

What is the name of the file which is used to declare custom attributes?

Create an XML res/values/attrs. xml file to define new attributes alongwith their data type.


1 Answers

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.

like image 112
Martin Nordholts Avatar answered Sep 22 '22 01:09

Martin Nordholts