gpt4 book ai didi

android - 在 Android 中声明可样式化的属性

转载 作者:IT老高 更新时间:2023-10-28 13:10:55 36 4
gpt4 key购买 nike

关于 declare-styleable 标签的文档很少,我们可以通过它为组件声明自定义样式。我确实找到了 this list attr 标签的 format 属性的有效值。虽然这很好,但它并没有解释如何使用其中一些值。浏览 attr.xml (标准属性的 Android 源代码),我发现您可以执行以下操作:

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

format 属性显然可以设置为值的组合。 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>

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

所以我有两个问题:

  1. 可以采用一组 enum 值中的一个的样式属性与可以采用一组 flag 值的样式属性有什么区别?
  2. 有没有人知道关于 declare-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:

<attr name="layout_scroll_height" format="integer">  
<enum name="scroll_to_top" value="-1"/>
</attr>

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更多提示。

您可以获得:

  • bool 值(getAttributeBooleanValue),
  • float (getAttributeFloatValue),
  • 整数(getAttributeIntValue),
  • ints(如 getAttributeUnsignedIntValue),
  • 和字符串(getAttributeValue)

关于android - 在 Android 中声明可样式化的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020379/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com