在属性格式中(以颜色为例),'color'和'color|reference'有什么区别?

5

简要版

我理解当设置默认主题时,为什么使用reference作为指向默认样式的属性格式,但是在定义颜色属性时,使用reference|color与仅使用color有何区别?您已经可以使用@color/xxx,它已经是对另一个资源的引用,因此是否隐含了引用?如果不是,那么一个用例比另一个更有用呢?

完整版

我一直在遵循以下推荐技术来主题化我们应用程序的自定义小部件的最佳实践。

在attrs.xml中

<!-- Attributes holding default styles -->
<attr name="myWidgetStyle"      format="reference" />
<attr name="myOtherWidgetStyle" format="reference" />

<!-- Custom attributes -->
<attr name="indicatorColor" format="color|reference" />

<!-- Assigning attributes to controls -->
<declare-styleable name="MyWidget">
    <item name="android:text" />
    <item name="indicatorColor" />
</declare-styleable>

<declare-styleable name="MyOtherWidget">
    <item name="android:text" />
    <item name="indicatorColor" />
</declare-styleable>

在 styles.xml 中
<style name="ThemeBase">

    <!-- Store default style in the style-reference attributes -->
    <item name="myWidgetStyle">@style/MyWidget</item>
    <item name="myOtherWidgetStyle">@style/MyOtherWidget</item>

    <!-- Reference primaryColor attribute when defining the value for this one -->
    <item name="indicatorColor">?primaryColor</item>
    <!-- alternate: <item name="indicatorColor">?attr/primaryColor</item> -->

</style>

<style name="ThemeA" parent="ThemeBase">
    <item name="primaryColor">@color/primaryColor_themeA</item>
</style>

<style name="ThemeB" parent="ThemeBase">
    <item name="primaryColor">@color/primaryColor_themeB</item>
</style>

最后,在我的小部件构造函数中,我根据需要将R.attr.myWidgetStyleR.attr.myOtherWidgetStyle传递给super的调用以及context.obtainStyledAttributes,然后可以按需获取定义的颜色。一切都按预期工作。我的控件得到了适当的主题。

然而,我想知道的是,有时在attrs.xml文件中会看到这个...

<attr name="indicatorColor" format="color" /> <-- Note no 'reference'

而且所有的东西都还能正常工作,这让我感到困惑,为什么你要写“color|reference”而不是只写“color”。我尝试了通过多个属性级联更改、以不同的顺序定义它们以及其他我能想到的任何方法来获得不同的结果,但它们都能正常工作。
那么有人能解释一下吗?
更好的是,有人能够提供一个示例,展示“color”和“color|reference”之间不同的行为吗?因为截至目前,我还没有找到这样的例子。

1
请在attrs.xml中查看<attr name="background" format="reference|color" /> - 它被注释为:*<!-- 用作背景的可绘制对象。这可以是对完整可绘制资源(例如PNG图像,9-patch,XML状态列表描述等)的引用,也可以是纯色,例如“#ff000000”(黑色)。-->* - pskink
啊啊啊...所以"color|reference"并不是说它可以接受颜色或颜色引用,而是说它可以接受颜色(直接/属性/资源)或者非颜色引用!换句话说,如果我们特定的属性始终只接受颜色,我们不需要也不应该加上“引用”部分,对吗? - Mark A. Donohoe
我进行了一些检查,似乎format确实有问题,请参见我的评论 - pskink
1个回答

4

属性格式仅用于系统知道此属性可以是哪种类型的资源。

color = 任何颜色。十六进制(#ffffffff)或链接到颜色资源(@color / supa-awesome_color)。这里不允许使用@drawable / mega_icon

reference = 任何引用(@color / supa_awesome_color,@drawable / mega_icon,@string / hi_there,...)

color | reference = 上述两者的联合体


Lint不会检查颜色引用。但是区别在于,AndroidStudio只会建议您使用'color'中的颜色以及'color|reference'中的颜色和引用。 - Anton Potapov
但是指定颜色资源不是一个引用吗?如果不是,那么什么才是颜色引用呢? - Mark A. Donohoe
1
是的,但如果一个属性只被指定为“颜色”而不是“颜色|引用”,它仍然可以使用@color引用,这就引出了我的问题...那么两者之间有什么区别呢?这就是为什么我要求提供代码示例来演示行为差异的原因。我开始认为它们之间没有区别,因为再次强调,“颜色”版本似乎可以很好地接受引用。 - Mark A. Donohoe
即使在您上面的示例中,您展示了只使用@color引用的颜色版本,这就是为什么我要问“'color'和'color|reference'之间有什么区别”的原因。我希望现在我的问题已经清楚明确了。 - Mark A. Donohoe
“这里不允许使用@drawable/mega_icon”并不准确,因为styles/styleables中的“format”已经损坏:我进行了一些额外的检查,如果你有“format=color”,你仍然可以传递“@drawable/mega_icon”,似乎对格式进行了一些检查,但我找不到任何简单的规则。 - pskink
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接