声明Android中设置样式属性样式、属性、声明、Android

2023-09-14 00:06:51 作者:如若愛請深愛

有关于声明-设置样式标记,使我们可以为组件声明自定义样式precious的小文件。我发现有效值这个列表为格式属性在 ATTR 标记。虽然这是很好的,只要它去,它没有解释如何使用其中的一些价值观。浏览attr.xml (Android的源标准属性),我发现,你可以做这样的事情:

 <! - 最突出的文字颜色。 - >
< attr指示NAME =textColorPrimary格式=引用|色/>
 

格式属性可以明显被设置为值的组合。 presumably的格式属性可帮助解析器间preT的实际样式值。后来我发现这attr.xml:

 <! - 默认的文本字体。 - >
< attr指示NAME =字体>
    <枚举名=正常值=0/>
    <枚举名=SANS值=1/>
    <枚举名=衬线值=2/>
    <枚举名=等宽值=3/>
< / ATTR>

<! - 默认的文本字体样式。 - >
< attr指示NAME =文字样式>
    <标志名称=正常值=0/>
    <标志名称=黑体值=1/>
    <标志名称=斜体值=2/>
< / ATTR>
 

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

所以,我有两个问题:

什么,可以采取一组枚举值,另一种可以在一组中的一个样式属性之间的区别标志值? 有谁知道的任何更好的文档如何声明-设置样式作品(除逆向工程的Andr​​oid源$ C ​​$ C)? 解决方案

还有这里的这个问题:定义定制ATTRS ,提供的部分的资料,但并不多。

这post 。它有大约国旗和枚举好消息:

  前端笔记 什么是盒模型

自定义XML属性标志

     

标记的特殊属性类型   它们只允许一个非常   值的小部分,即那些   所下的定义   属性标记。标志由指定   一个name属性和价值   属性。的名字被要求   是,属性类型中是唯一的   但数值不必。这是   其原因是,在进化过程中   Android平台,我们有   FILL_PARENT和match_parent既   映射到相同的行为。其   值是相同的。

     

name属性映射到名字   在该范围内的值的地方使用   布局XML和不需要   命名空间preFIX。因此,对于   tilingMode上面我选择了中心   的属性值。我可以有   很容易地选择了拉伸或   重复,但没有别的。不   即使在代的实际值   将被允许​​的。

     

value属性必须是   整数。十六进制的选择或   标准的数字再presentation达   给你。还有在一些地方   在Android code其中两者都使用   和Android的编译器很高兴   接受无论是。

     

自定义XML属性枚举

     

枚举在几乎相同的使用   地标志一个规定,   它们可以互换使用   整数。引擎盖下枚举和   整数被映射到相同的数据   型,即一个整数。什么时候   出现在属性定义中   为整数,枚举起到prevent   幻数,这都是坏的。   这就是为什么你可以有一个   机器人:layout_width与任何一个   尺寸,整数,或指定的字符串   FILL_PARENT。

     

要放在这个背景下进入,让我们   假设我创建一个自定义   属性称为   layout_scroll_height它接受   整数或一个串   scroll_to_top。要做到这一点我想补充一个   整数的格式属性,并按照   与枚举:

 < attr指示NAME =layout_scroll_height格式=整数>
    <枚举名=scroll_to_top值= -  1/>
< / ATTR>
 

     

使用枚举当单规定   以这种方式是,一个显影剂   使用自定义视图可以   有目的地将值-1到   布局参数。这会   触发的特殊情况下的逻辑   scroll_to_top。这种意外(或   预期)的行为可能会很快   贬谪您的图书馆的遗产   code一堆如果枚举值分别为   选择不当的。

     

在我看来,真正的价值,你可以在现实中加入由你可以从中获取有限的属性。此处检查的AttributeSet 类引用更多的提示。

您可以获取:

布尔( getAttributeBooleanValue ), 花车( getAttributeFloatValue ), 整型( getAttributeIntValue ), 在整数(如 getAttributeUnsignedIntValue ), 和字符串( getAttributeValue

There's precious little documentation about the declare-styleable tag by which we can declare custom styles for components. I did find this list of valid values for the format attribute of the attr tag. While that's nice as far as it goes, it doesn't explain how to use some of those values. Browsing attr.xml (the Android source for the standard attributes), I discovered that you can do things like:

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

The format attribute can evidently be set to a combination of values. Presumably the format attribute helps the parser interpret an actual style value. Then I discovered this in 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>

Both of these seem to declare a set of allowed values for the indicated style.

So I have two questions:

What's the difference between a style attribute that can take on one of a set of enum values and one that can take on a set of flag values? Does anyone know of any better documentation for how declare-styleable works (other than reverse engineering the Android source code)?

解决方案

There's this question here: Defining custom attrs with some info, but not much.

And this post . It has good info about flags and enums:

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.

As I see it, the real values you can add in reality to an attribute is limited by what you can obtain from it. Check the AttributeSet class reference here for more hints.

You can obtain:

booleans (getAttributeBooleanValue), floats (getAttributeFloatValue), ints (getAttributeIntValue), ints (as getAttributeUnsignedIntValue), and strings (getAttributeValue)