针对不同Android SDK版本的不同主题

时间:2012-02-04 10:17:34

标签: android android-layout android-theme

根据安装应用程序的SDK版本,有没有办法使用不同的主题?

我问的原因是因为我想支持一直回到SDK版本8,但对于那些拥有ICS的用户,我希望能够遵循ICS的设计标准并使用Holo主题。

我可以从Program different layouts for different versions in android看到我可以有一个文件夹值-v14,它将有一个theme.xml来覆盖主题声明。但是,如果我引用Theme.Holo,它将无法编译。我相信这是因为我的AndroidManifest.xml中有以下内容

<uses-sdk android:minSdkVersion="8" />
<uses-sdk android:targetSdkVersion="11"/>

任何指针都会非常感激。

更新: - 好的,所以这是我的文件: - 的AndroidManifest.xml:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name=".Refunder"
    android:theme="@style/MainTheme"
    >

RES /值/的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:typeface">normal</item>
        <item name="android:textSize">15sp</item>
    </style>
</resources>

RES /值-V11 /的themes.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MainTheme" parent="@android:style/Theme.Holo">
        <item name="android:typeface">normal</item>
        <item name="android:textSize">15sp</item>
    </style>
</resources>

这与我在此处阅读的文章一致: - http://android-developers.blogspot.com/2012/01/holo-everywhere.html

当我这样做时,我在Eclipse中遇到编译错误,说: -

error: Error retrieving parent for item: No resource found that matches the given name '@android:style/Theme.Holo'

1 个答案:

答案 0 :(得分:15)

您正在做什么(使用您的values-v14文件夹)是正确的。您只需要更改Build Target以允许它进行编译。 (右键单击您的项目,选择属性,选择Android,选择Android 14或更高版本)

请确保您不使用任何超过Android:minSdkVersion的功能,因为如果在早期版本的Android上使用它会导致强制关闭。