Skip to content Skip to sidebar Skip to footer

Can Someone Explain Me Declare-styleable Xml Tag In This Example And The Theory Behind Its Use?

I'm reading Beginning Android 4 Development and in chapter 5 it talks about Gallery and ImageVievs and introduces the declare-styleable XML tag without explaining its purpose.. I t

Solution 1:

This tag is part of a set of premade Android attributes defined in R.Styleable, which can be told apart from a custom styleable tag from the android: xml namespace prefix before the attribute name.

This particular attribute is described as:

The preferred background for gallery items. This should be set as the background of any Views you provide from the Adapter.

You are right, however, that custom attribute tags would require not only the name of the attribute but what its type is, for example adding a custom element to your attrs.xml file might look like this:

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleablename="MyCustomView"><attrname=”android:galleryItemBackground” /><attrname="myCustomAttr"format="string" /></declare-styleable></resources>

Note the lack of the android: namespace on the second attribute as well.

Edit:

Is there any official documentation page that explains in depth this Styleables?

Check out R.attr (click for link) for various attributes included in Android. You do not need to declare a type for them, because they are all already declared. To know what type has been declared for a particular attribute, find the description for the one you are interested in. galleryItemBackground is, as you may expect, a reference to another resource; other possibilities are booleans, floats, colors, etc.

Additional references: Andtoid uses the <declare-styleable> tag to create an AttributeSet. TypedArray is used to parse the AttributeSet.

If the purpose of the code above [...] is simply get a default Drawable for the view's background, couldn't I set the variable itemBackground with getDrawable(android.R.attr.galleryItemBackground)?

In the example, it is hard to see the usefulness of this pattern when there is only one attribute. You can do what you ask, and it may be easier. The construct, however, is part of Android's mantra to separate the UI's "look" from its "functionaly" by letting you set certain attributes in xml instead of having to do everything in code. Take the View class, for example. It has over 30 attributes that can be set in an xml file (size, padding, clickable, focusable, etc); someone making a custom subclass of View can set a few, all or none of these attributes in xml and they are automatically handled for you when the view is created. There are code equivalents to set the attributes if needed, but imagine every time you subclassed View you HAD to set all attributes in code instead of having an option to set them in xml.

It would also be a trivial matter to just make your own resources for your classes that do exactly the same thing, however using the built in styles will provide default resources that match the look and feel of the Android framework if you do not override them.

Hope this helps.

Post a Comment for "Can Someone Explain Me Declare-styleable Xml Tag In This Example And The Theory Behind Its Use?"