使用不同的图标不同的Andr​​oid SDK版本不同、图标、版本、Andr

2023-09-12 02:01:37 作者:哥的帅气、值得你拥有

我有图标我的Andr​​oid菜单。在Android上3+我使用了黑色的ActionBar这样的图标是白色的。但是,在Android 2.X菜单本身是白色的,这意味着图标都几乎看不见。如何使用不同的菜单图标不同的版本?我假设我可以使用像RES /绘制-MDPI-V11不同的绘制目录做到这一点,但我不知道是否有另一种方式,所以我不必创建一堆不同的目录,因为我添加的版本或像素密度。

I have icons for my Android menu. On Android 3+ I'm using a black ActionBar so the icons are white. However, on Android 2.x the menu is inherently white which means the icons are nearly invisible. How can I use different menu icons for different versions? I'm assuming I can do it using different drawable directories like res/drawable-mdpi-v11, but I'm wondering if there is another way so I don't have to create a bunch of different directories as I add versions or pixel densities.

编辑:我把RES /绘制-MDPI和RES /可绘制,华电国际为与Android 2.X使用深色版本,我放轻版本RES /绘制-MDPI-V11和RES /绘制-HDPI-V11的与Android 3.x和更高的,但我的Andr​​oid 2.1(SDK 7)模拟器仍呈现光的版本使用。

I put dark versions in res/drawable-mdpi and res/drawable-hdpi for use with Android 2.x and I put light versions in res/drawable-mdpi-v11 and res/drawable-hdpi-v11 for use with Android 3.x and higher, but my Android 2.1 (sdk 7) emulator is still showing the light version.

任何想法,为什么?

推荐答案

您可以根据平台版本选择一个主题,如的样式和主题开发指南。定义一个风格在你的RES /价值/ styles.xml是这样的:

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>

然后在RES /值-V11 /文件夹,选择你的主题(也许全息,如果你是黑)

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

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

然后添加图标的样式。举例来说,这里是从HoneycombGallery示例应用程序。

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

底部的3个元素都在绘制目录中的所有图标。您仍然需要每分辨率组特定的图标至少有一个文件夹,但你可以结合灯光和安培;黑暗图标放到同一个文件夹,但你不会有有图标的每个平台版本不同的文件夹。此外,你需要列出它们在价值/ attrs.xml文件引用,像这样的:

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>

在这一点上,你就可以使用间接引用来引用它们的布局XML中,类似这样的ATTR / NameOfYou​​rDrawable?

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