Hi,

I'm creating my own View class, and defining custom xml attributes
with a attrs.xml. As long as I provide each attribute manually, there
is no problem, but

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <declare-styleable id="my.package.CustomizedButtonView"
name="CustomizedButtonView">
                <attr name="borderDrawable" format="reference|color"/>
        </declare-styleable>
</resources>

To be honest, I don't understand the proper use of the attributes "id"
and "name" in the above code, but somehow it works. I can now
reference the borderDrawable-Attribute from a layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android";
        xmlns:mypack="http://some.weird.url.com/seems/not/to/matter";

        <my.package.CustomizedButtonView
                                android:text="This is my text"
                                mypack:borderDrawable="@drawable/border243"/>
</LinearLayout>

My class CustomizedButtonView now reads the attribute like this:
        public CustomizedButtonView(Context context, AttributeSet attrs) {
                super(context, attrs);
                int borderDrawableId = attrs.getAttributeResourceValue(
                                "http://some.weird.url.com/seems/not/to/matter";,
                                "borderDrawable", -1);

Until now, everything works fine. Many of my CustomizedButtonView will
have the same borderDrawable, but not all of them, so I want to define
a style. My styles.xml reads like this:

<style name="customStyle1">
                <item
                        name="android:text">Default text</item>
                <item
                        name="mypack:borderDrawable">@drawable/border243</item>
        </style>

And the layout like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android";
        xmlns:mypack="http://some.weird.url.com/seems/not/to/matter";

        <my.package.CustomizedButtonView
                                style="@style/customStyle1"/>
</LinearLayout>

The android:text is properly set in my instance, but the
borderDrawable is not. I guess this has something to do with
namespaces, because inside the styles.xml, the
name="mypack:borderDrawable" is not handled by the XML parser's
namespace facility, because its inside an attribute value. So "mypack"
is in no way connected to "http://some.weird.url.com/seems/not/to/
matter" and adding it via xmlns:mypack... to the stylefile would not
help, I guess. In the same file, "android:text" is somehow recognized,
even though "android" is AFAIK only a ns-defintion for "http://
schemas.android.com/apk/res/android", which is also not declared in
that file.

So what is the proper way to set a custom attribute in a style?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to