首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓AppCompat主题

安卓AppCompat主题
EN

Stack Overflow用户
提问于 2014-04-14 18:09:15
回答 2查看 9.2K关注 0票数 1

我正在运行Android开发人员类,因为我想开始构建一些应用程序。

我已经下载了与Eclipse一起使用的ADT包,因为我熟悉Eclipse。

我正在玩弄主题,但是出于某种原因,我的项目被设置为只允许AppCompat主题。这意味着我必须使用像Theme.AppCompat.Light这样的主题。如果我尝试使用Theme.Holo或Theme.Translucent,我会得到一个运行时错误: IllegalStateException:您需要使用Theme.AppCompat主题

我在AndroidManifest.xml中设置了sdk的最低版本,如下所示:

代码语言:javascript
复制
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

感谢您的帮助。

在Guillermo的回复之后编辑:

谢谢Guillermo,但这并不管用。以下是更新后的AndroidManifest:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyBaseTheme2" >
    <activity
        android:name="com.example.myfirstapp.MainActivity"
        android:label="@string/app_name" 
        android:theme="@style/MyBaseTheme1">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity"  
        android:theme="@style/MyBaseTheme2">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>
</manifest>

我在res/ styles.xml -14中有以下值文件:

代码语言:javascript
复制
<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="MyBaseTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>
<style name="MyBaseTheme2" parent="android:Theme.Holo.Light">
</style>

</resources>

我在我的第一个活动中使用MyBaseTheme1,它加载得很好。我在第二个活动中使用了MyBaseTheme2,这会使应用程序崩溃。如果我在第二个活动中使用MyBaseTheme1,就可以很好地工作,不会发生崩溃。

编辑以添加样式文件。res/values/styles.xml:

代码语言:javascript
复制
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="MyBaseTheme1" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="MyBaseTheme1">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

res/values-11/styles.xml

代码语言:javascript
复制
<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="MyBaseTheme1" parent="Theme.AppCompat.Light">
        <!-- API 11 theme customizations can go here. -->
    </style>

</resources>
EN

回答 2

Stack Overflow用户

发布于 2014-05-22 13:00:11

在您尝试应用不同主题的活动中,导入android.app.Activity。

还要确保该类扩展了Activity,而不是ActionBarActivity。

当类扩展ActionBarActivity时,它被限制为AppCompat.Light主题

票数 3
EN

Stack Overflow用户

发布于 2014-04-14 18:25:38

全息主题是在API14中添加的,如果你使用minSdkVersion="11",你将被迫使用API11兼容的资源。

如果你仍然想保持你的应用程序与API11兼容,你可以使用HoloEverywhere,更多信息here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23057372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档