繁体   English   中英

Android AppCompat主题

[英]Android AppCompat Themes

我正在遍历Android Developer类,因为我想开始构建一些应用程序。

我已经对Eclipse熟悉,因此我已经通过Eclipse下载了ADT软件包。

我在玩主题,但是由于某种原因,我的项目已设置为仅允许AppCompat主题。 这意味着我必须使用Theme.AppCompat.Light之类的主题。 如果尝试使用Theme.Holo或Theme.Translucent,则会收到运行时错误:IllegalStateException:您需要使用Theme.AppCompat主题

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

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

任何帮助表示赞赏。

吉列尔莫回应后编辑:

谢谢吉列尔莫,但这没有用。 这是更新的AndroidManifest:

<?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 / values-14中具有以下styles.xml文件:

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

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

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

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

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

当类扩展ActionBarActivity时,它将仅限于AppCompat.Light主题

Holo主题已添加到API 14中,如果您使用minSdkVersion="11" ,则将被迫使用与API 11兼容的资源。

如果您仍想使您的应用程序与API 11兼容,则可以使用HoloEverywhere ,有关更多信息,请点击此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM