Custom attributes in styles.xml Ask Question

Custom attributes in styles.xml Ask Question

I have created a custom widget, and I'm declaring it in layout.xml. I have also added some custom attributes in attr.xml. However, when trying to declare these attributes in a style in styles.xml, it's giving me No resource found that matches the given name: attr 'custom:attribute'.

I have put the xmlns:custom="http://schemas.android.com/apk/res/com.my.package" in all of the tags in styles.xml, including <?xml>, <resources>, and <style>, but it still gives me the same error, that it can't find my custom XML namespace.

I can, however, use my namespace to manually assign attributes to the view in my layout.xml, so there is nothing wrong with the namespace. My issue lies in making styles.xml aware of my attr.xml.

ベストアンサー1

I figured it out! The answer is to NOT specify the namespace in the style.

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>

        <item name="custom_attr">value</item> <!-- tee hee -->
    </style>
</resources>

おすすめ記事