【问题标题】:What's the difference between using ? and @ as style-qualifier?使用 和有什么区别?和 @ 作为样式限定符?
【发布时间】:2015-04-08 09:54:38
【问题描述】:

为 style-attributes 设置一个单独的 ?-qualifier 的目的是什么?为什么不使用@-qualifier?

    <Button
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/ic_media_play" />  

【问题讨论】:

标签: android styles


【解决方案1】:

样式属性资源允许您引用当前应用主题中的属性值。引用样式属性允许您通过设置样式以匹配当前主题提供的标准变体来自定义 UI 元素的外观,而不是提供硬编码的值。引用样式属性本质上是说,“在当前主题中使用该属性定义的样式。”

要引用样式属性,名称语法几乎与正常资源格式相同,但使用问号 (?) 代替 at 符号 (@),并且资源类型部分是可选的。例如:

例如,以下是如何引用属性来设置文本颜色以匹配系统主题的“主要”文本颜色:

<EditText id="text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="?android:textColorSecondary"
    android:text="@string/hello_world" />

这里,android:textColor 属性指定了当前主题中的样式属性的名称。 Android 现在使用应用于 android:textColorSecondary 样式属性的值作为此小部件中 android:textColor 的值。由于系统资源工具知道在此上下文中需要属性资源,因此您无需显式声明类型(应为 ?android:attr/textColorSecondary)——您可以排除 attr 类型。

Source: HERE

【讨论】:

  • 感谢这个精确的解释。我认为这个主题(风格和主题)非常重要,但大多数作者都没有很好地记录(即使developer.android.com 上的在线文档也不完整)。我也认为,一些作者混淆了“风格”这个词。对我来说,样式是属性值对的命名集合。然而,有些人将此称为样式属性或属性值,因此混淆是完美的。
猜你喜欢
  • 2018-11-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 2023-03-10
相关资源
最近更新 更多