Difference between android:icon=“@drawable/my_icon” and android:icon=“?my_icon”? [duplicate]

筅森魡賤 提交于 2020-01-13 11:00:07

问题


I usually set drawable in xml as android:icon="@drawable/my_icon" But in some projects I see the code android:icon="?my_icon".

What's the difference between android:icon="@drawable/my_icon" and android:icon="?my_icon" ?


回答1:


Pointing to this document and referencing the answer from there: Applying styles and themes

Just like styles, themes are also declared in XML elements, and are referenced in the same manner. The difference is that you add a theme to an entire application or activity, via the and elements in the Android Manifest — themes cannot be applied to individual Views.

Lets take an example declaration of a theme defined on the link:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="CustomTheme">  
    <item name="windowBackground">@drawable/screen_background_white</item>
    <item name="panelForegroundColor">#FF000000</item>
    <item name="panelBackgroundColor">#FFFFFFFF</item>      
    <item name="panelTextColor">?panelForegroundColor</item>
    <item name="panelTextSize">14</item>
    <item name="menuItemTextColor">?panelTextColor</item>
    <item name="menuItemTextSize">?panelTextSize</item>
  </style>
</resources>

Notice the use of the at-symbol (@) and the question-mark (?) to reference resources. The at-symbol indicates that we're referencing a resource previously defined elsewhere (which may be from this project or from the Android framework). (E.g., panelTextColor uses the same color assigned to panelForegroundColor, defined beforehand.) This technique can be used only in XML resources.

The question-mark indicates that we're referencing a resource value in the currently loaded theme. This is done by referring to a specific by its name value.

So, if you see that menuItemTextColor point to another item panelTextColor which again has question mark in front of its value. Why? Because we are again referencing a resource value from the currently loaded customTheme.

Similarly although you haven't mentioned any code but its possible that the currently loaded theme has item called my_icon whose value references a resource value pointing to some drawable in the project.

Hope this clears you a bit.



来源:https://stackoverflow.com/questions/21039310/difference-between-androidicon-drawable-my-icon-and-androidicon-my-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!