Use different icons with different Android SDK versions

时光毁灭记忆、已成空白 提交于 2019-12-02 17:18:45

You can Select a theme based on platform version, as outlined in the Styles and Themes dev guide. Define a style in your res/values/styles.xml like this:

<style name="ThemeSelector" parent="android:Theme.Light">
    ...
</style>

Then in a res/values-v11/ folder, select your theme (probably Holo, if you're dark)

<style name="ThemeSelector" parent="android:Theme.Holo">
    ...
</style>

Then add icons to that style. For instance, here's a snippet from the styles.xml file from the HoneycombGallery sample application.

<style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
    ...
    <item name="menuIconCamera">@drawable/ic_menu_camera_holo_dark</item>
    <item name="menuIconToggle">@drawable/ic_menu_toggle_holo_dark</item>
    <item name="menuIconShare">@drawable/ic_menu_share_holo_dark</item>
</style>

The bottom 3 elements are all icons in the drawable directories. You'll still need at least one folder per resolution-specific set of icons, but you can combine the light & dark icons into the same folder, but you won't have to have different folders of icons for each platform version. Also, you'll need to list them as references in the values/attrs.xml file, like this:

<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>

At which point you'll be able to refer to them within your layout XML using the "?attr/NameOfYourDrawable" dereference, like this:

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

Found on the android dev site: http://developer.android.com/guide/practices/ui_guidelines/icon_design_menu.html

Warning: Because these resources can change between platform versions, you should not reference these icons using the Android platform resource IDs (i.e. menu icons under android.R.drawable). If you want to use any icons or other internal drawable resources, you should store a local copy of those icons or drawables in your application resources, then reference the local copy from your application code. In that way, you can maintain control over the appearance of your icons, even if the system's copy changes. Note that the grid below is not intended to be complete.

/res/drawable-hdpi (for Android 2.2 and below)

/res/drawable-hdpi-v# (for Android 2.3 and above)

Have you also tried testing this on a 2.1+ phone and not an emulator? If you don't have a phone, try creating another AVD? I'm afraid that you're going to need the separate folders.

Hopefully this helps.

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