现在的位置: 首页 > 综合 > 正文

Valid format values for declare-styleable/attr tags

2012年12月19日 ⁄ 综合 ⁄ 共 1275字 ⁄ 字号 评论关闭

Meh. Wasted an hour trying to sort this out.

The answer happened to be buried in some Android code. The fact that Android is open source is a cool thing–but I shouldn’t have to dig through that source kit just to figure out the answer to something that could be better documented.

When declaring a <declare-styleable> <attr> tag, you supply a name for the new attribute for your custom class, and you provide a format. Well, I couldn’t find the valid values for the format attribute in the documentation. But I did find them here.

And those values?

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

It also appears from the source code that this field is optional, and I presume if it is left blank, either one of two things will happen: this will default as a resource reference, or the format is only used for type checking, and this can be any value. I dunno; I haven’t tried it.

On the off-chance someone knows the answer, could they leave it in the comments?

 

 

In my application I have the same custom attributes for a custom ImageView and for a custom TextView (selected_image and unselected_image). I would like to avoid as much as possible duplicate code. So I was thinking if there is a way of declaring the common attributes just once and ise it in both views. Here is the code from the attrs.xml file:

<declare-styleable name="SelectableImageView">
   
<attr name="selected_image" format="integer"/>
   
<attr name="unselected_image" format="integer"/>
</declare-styleable>

Thanks a lot,

Gratzi

【上篇】
【下篇】

抱歉!评论已关闭.