【问题标题】:Can someone explain the attr?有人可以解释一下attr吗?
【发布时间】:2011-11-22 05:29:00
【问题描述】:

我正在查看 Honeycomb Gallery 示例代码 (here),并在尝试在自己的应用中添加操作项时遇到了以下代码:

<item android:id="@+id/camera"
    android:title="Camera"
    android:icon="?attr/menuIconCamera"
    android:showAsAction="ifRoom" />

?attr 让我陷入了循环。有人可以解释这是在做什么吗?这与可绘制对象有什么关系?我似乎无法在 Google 上找到任何好的信息。还有我们可以为图标使用的属性列表或库,而不仅仅是menuIconCamera

谢谢

编辑: 我又环顾四周,发现 attrs.xml 看起来像这样:

<resources>
<declare-styleable name="AppTheme">
    <attr name="listDragShadowBackground" format="reference" />
    <attr name="menuIconCamera" format="reference" />
    <attr name="menuIconToggle" format="reference" />
    <attr name="menuIconShare" format="reference" />
</declare-styleable>

不幸的是,这让我更加困惑。这是在做什么?

【问题讨论】:

    标签: android android-layout attributes android-theme


    【解决方案1】:

    ?attr/menuIconCamera 值表示将使用来自当前主题的menuIconCamera 属性的图标。

    themes.xml 文件中的某处必须有一个可绘制对象分配给 menuIconCamera 属性。如果有两个主题具有不同的此属性值,则实际图标将取决于当前使用的主题。

    attrs.xml 文件用于定义自定义属性。如果没有这个定义,编译器会将未知属性视为错误。

    【讨论】:

    • 你说得对,@drawable/ic_menu_camera_holo_light,非常感谢。我看到 ic_menu_camera_holo_light 是本地可绘制对象。 3.x 没有像 2.x 那样内置的公共图标吗?
    • 我不认为它以某种方式连接到 Android 版本。这只是使属性依赖于所选主题的一种方式。
    【解决方案2】:

    ?attr: 语法用于访问当前主题的属性。见referencing style attributes

    【讨论】:

    • 提供的链接非常非常有用。谢谢!
    • 非常有帮助,但您仍应发布该链接的主要部分。
    • 这是链接文章中最有用的部分:'样式属性资源允许您引用当前应用主题中的属性值。引用样式属性允许您通过设置样式以匹配当前主题提供的标准变体来自定义 UI 元素的外观,而不是提供硬编码的值。引用样式属性本质上是说,“在当前主题中使用此属性定义的样式。”'
    【解决方案3】:

    我知道这篇文章很老了,但我觉得下面的解释可以帮助初学者轻松理解它。

    所以通俗地说,

    someAttribute="?attr/attributeName" 表示-

    someAttribute 的值设置为当前主题中 attributeName 的值

    一个常见的例子是设置工具栏的样式

    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimary">@color/primary_color</item>
           //some more stuff here
    </style>
    <!-- custom toolbar style -->
    <style name="myToolbar" parent="Widget.AppCompat.Toolbar">
          <item name="android:background">?attr/colorPrimary</item>
         //some code here
    </style>
    

    此处android:background 的值将设置为@color/primary_color,因为?attr/colorPrimary 指的是当前主题(AppTheme)中的@color/primary_color

    【讨论】:

      【解决方案4】:

      我的英语不好,抱歉。但我知道这个问题

      android:icon="?attr/menuIconCamera"想要使用

      attrs.xml

      <resources>
          <declare-styleable name="AppTheme">
              <attr name="listDragShadowBackground" format="reference" />
              <attr name="menuIconCamera" format="reference" />
              <attr name="menuIconToggle" format="reference" />
              <attr name="menuIconShare" format="reference" />
          </declare-styleable>
      </resources>
      

      styles.xml

      <style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light">
              <item name="android:actionBarStyle">@style/ActionBar.Light</item>
              <item name="android:windowActionBarOverlay">true</item>
              <item name="listDragShadowBackground">@android:color/background_light</item>
              <item name="menuIconCamera">@drawable/ic_menu_camera_holo_light</item> //this....
              <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_light</item>
              <item name="menuIconShare">@drawable/ic_menu_share_holo_light</item>
          </style>
      

      使用@drawable/ic_menu_camera_holo_light

      【讨论】:

        【解决方案5】:

        这是用于引用样式属性。见 R.attr

        ?[<package_name>:][<resource_type>/]<resource_name>
        

        Referencing style attributes

        【讨论】:

        • 似乎没有在任何地方记录的一件事是“”是声明资源的任何内容的完整包名称。更具体地说,它不是 XML 命名空间前缀,尽管语法可能暗示了这一点。例如,要引用 appcompat 库声明的 attr,请使用 android.support.v7.appcompat:
        【解决方案6】:

        这篇博文非常出色地介绍了如何引用当前主题中定义的样式属性的值: https://trickyandroid.com/android-resources-and-style-attributes-cheatsheet/

        • 当您看到 ? 表示法时 - 这意味着我们正在尝试引用样式属性 - 一个值 这可能因当前主题而异。在每个特定的主题中,我们都可以覆盖这个属性,所以 XML 布局不需要更改,并且需要应用正确的主题。

        • 当您看到 @ 符号时 - 我们引用实际资源值(颜色、字符串、尺寸、 等等)。该资源应具有实际价值。在这种情况下,我们确切地知道我们的价值是什么 处理。

        这是一个例子:

            <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
                <!-- Customize your theme here. -->
                <item name="colorPrimary">@color/colorPrimary</item>
                <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
                <item name="colorAccent">@color/colorAccent</item>
            </style>
        
            <style name="LauncherButton" parent="TextAppearance.AppCompat.Medium">
                <item name="android:textColor">?colorAccent</item>
                <item name="android:layout_width">match_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_centerHorizontal">true</item>
                <item name="android:textAllCaps">false</item>
            </style>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-12
          • 2011-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-21
          相关资源
          最近更新 更多