在Android中声明styleable属性

有很多关于declare-styleable标签的文档,我们可以通过它来声明组件的自定义样式。我找到attr标签的格式属性的有效值的this list。虽然这是很好的,尽管它是,它不解释如何使用一些这些值。浏览attr.xml(Android源代码的标准属性),我发现你可以做以下事情:

<!-- The most prominent text color.  -->
<attr name="textColorPrimary" format="reference|color" />

format属性可以明显地设置为值的组合。大概格式属性帮助解析器解释实际的样式值。然后我在attr.xml中发现了这个:

<!-- Default text typeface. -->
<attr name="typeface">
    <enum name="normal" value="0" />
    <enum name="sans" value="1" />
    <enum name="serif" value="2" />
    <enum name="monospace" value="3" />
</attr>

<!-- Default text typeface style. -->
<attr name="textStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>

这两个都似乎声明一组允许的值为指定的样式。

所以我有两个问题:

>可以采用一组枚举值中的一个的样式属性与可以采用一组标志值的样式属性之间有什么区别?
>有没有人知道任何更好的文档如何声明styleable工作(除了逆向工程的Android源代码)?

这里有这个问题:Defining custom attrs有一些信息,但不是很多。

和这个post。它有关于标志和枚举的好信息:

Custom XML Attribute Flags

Flags are special attribute types in
that they are allowed only a very
small subset of values, namely those
that are defined underneath the
attribute tag. Flags are specified by
a “name” attribute and a “value”
attribute. The names are required to
be unique within that attribute type
but the values need not be. This is
the reason that during the evolution
of the Android platform we had
“fill_parent” and “match_parent” both
mapping to the same behavior. Their
values were identical.

The name attribute maps to the name
used in the value place within the
layout XML and does not require a
namespace prefix. Hence, for the
“tilingMode” above I chose “center” as
the attribute value. I could have
just as easily chosen “stretched” or
“repeating” but nothing else. Not
even substituting in the actual values
would have been allowed.

The value attribute must be an
integer. The choice of hexadecimal or
standard numeral representation is up
to you. There’s a few places within
the Android code where both are used
and the Android compiler is happy to
accept either.

Custom XML Attribute Enums

Enums are used in an almost identical
manner as flags with one provision,
they may be used interchangeably with
integers. Under the hood Enums and
Integers are mapped to the same data
type, namely, an Integer. When
appearing in the attribute definition
with Integers, Enums serve to prevent
“magic numbers” which are always bad.
This is why you can have an
“android:layout_width” with either a
dimension, integer, or named string
“fill_parent.”

To put this into context, let’s
suppose that I create a custom
attribute called
“layout_scroll_height” which accepts
either an integer or a string
“scroll_to_top.” To do so I’d add an
“integer” format attribute and follow
that with the enum:

06000

The one stipulation when using Enums
in this manner is that a developer
using your custom View could
purposefully place the value “-1″ into
the layout parameters. This would
trigger the special case logic of
“scroll_to_top.” Such unexpected (or
expected) behavior could quickly
relegate your library to the “legacy
code” pile if the Enum values were
chosen poorly.


正如我看到的,你可以添加到一个属性的实际价值取决于你可以从中获得的东西。检查AttributeSet类引用here以获得更多提示。

您可以获得:

> booleans(getAttributeBooleanValue),
> float(getAttributeFloatValue),
> ints(getAttributeIntValue),
> ints(as getAttributeUnsignedIntValue),
>和字符串(getAttributeValue)

http://stackoverflow.com/questions/6020379/declaring-styleable-attributes-in-android

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:在Android中声明styleable属性