在非主题样式中访问同级样式属性
属性是否可以引用其同级属性,即存在于同一 中的属性?
例如,
<style name="Widget.A">
<item name="customBackground">something</item>
<item name="android:background">?customBackground</item>
</style>
会产生错误,因为 ?
限定符仅引用应用主题中存在的属性,而不是同一样式中存在的属性。
为了与所有版本的 Android 正确兼容,此功能对于我的库来说是必需的。
Is it possible for an attribute to reference its sibling attributes, that is, ones that exist in the same <style>
?
For example,
<style name="Widget.A">
<item name="customBackground">something</item>
<item name="android:background">?customBackground</item>
</style>
will produce an error since the ?
qualifier references only attributes which exist in applied theme, not within the same style.
This functionality is a necessity for my library in order for proper compatibility with all versions of Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,那个?语法仅允许引用当前主题的属性。您必须重新编写样式才能在自定义主题中定义该值。
No, the ? syntax only allows references attributes from the current theme. You would have to re-write your style to define that value in a custom theme.
您的代码会产生错误,因为没有已知的属性“customBackground”。但是您可以通过在同一文件夹 (
res/values/
) 中创建一个包含以下内容的 XML 文件来创建它:此声明告诉我们您有一个样式 Widget.A< /em>,它有一个名为 customBackground 的参数,在此示例中,其有效值是对其他属性的引用。当然,您可以将
format
属性设置为您需要的任何内容。不幸的是,Android 文档没有包含此类声明的明确描述,因此我建议您查看 attrs.xml 。Your code produces an error because there's no known attribute "customBackground". But you can create it by making an XML file in the same folder (
res/values/
) with the following contents:This declaration tells us that you've got a style Widget.A, which has a parameter called customBackground, and in this example its valid values are references to other attributes. Of course, you can set the
format
attribute to whatever you need. Unfortunately, Android documentation doesn't contain clear description for this kind of declarations, so I advise you to look at the attrs.xml in the Android source code.