If you are using attrs.getAttributeResourceValue(), then you are not using
the style/theming system at all.  That is, there was no purpose to defining
a <declare-styleable> an <attr> at all, that function is just retrieving a
raw value from a resource for a given name.  (This is also why the "
http://some.weird.url.com/seems/not/to/matter"; URI doesn't matter -- you
have declared it as a namespace in your XML, and you have retrieved the
attribute in that namespace in your Java code, and that is all that is
happening.  You just made up a namespace and used it.)

You can find a complete example if declaring styleables and themes in API
demos.  Basically you declare a styleable like this (there is no "id" field
that is used):

<declare-styleable name="LabelView">
  <attr name="text" format="string"/>
  <attr name="textColor" format="color"/>
  <attr name="textSize" format="dimension"/>
  </declare-styleable>
</resources>

Then use it in a layout like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";

        xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis";

        android:orientation="vertical"
        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

    <com.example.android.apis.view.LabelView

            android:background="@drawable/green"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            app:text="Green" app:textColor="#ffffffff" />

</LinearLayout>


Note that the URI of xmlns:app is very important, this is the namespace for
android resources (the first part) in your package (the second part).

Finally to retrieve the resources, you do this:

    public LabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLabelView();

        TypedArray a = context.obtainStyledAttributes(attrs,

                R.styleable.LabelView);

        CharSequence s = a.getString(R.styleable.LabelView_text);
        if (s != null) {
            setText(s.toString());
        }

        // Retrieve the color(s) to be used for this view and apply them.

        // Note, if you only care about supporting a single color, that you
        // can instead call a.getColor() and pass that to setTextColor().
        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));

        int textSize =
a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }

        a.recycle();
    }

The use of obtainedStyledAttributes() is very important, as that is the key
function that extracts the attributes you have declared in your styleable,
filling them in from the current AttributeSet or style or theme as
appropriate.

With all of that together, you can now define your theme, assigning values
to your own custom attributes that will be seen in your custom view.

On Wed, Jul 8, 2009 at 8:02 AM, brian.schim...@googlemail.com <
brian.schim...@googlemail.com> wrote:

>
> 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?
> >
>


-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--~--~---------~--~----~------------~-------~--~----~
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