无法将 Theme.Holo 用于我的第一个 Android 应用的样式

Can't use Theme.Holo for styles of my first Android app

我正在按照官方 Android 教程开发我的第一个 Android 应用程序,所以我是新手,但是当我尝试使用 Theme.Holo 我的应用程序在启动过程中崩溃,进入日志我有以下消息:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我知道如果我使用 Theme.AppCompact 它是有效的,但是在教程中没有提到像 Theme.Holo 这样的问题,也没有给出解决方案,因为如果我使用Theme.AppCompact 应用可以运行,但我无法使用自定义颜色或背景图片修改操作栏的样式。

到目前为止,我一直完全按照教程进行操作:https://developer.android.com/training/basics/actionbar/styling.html minSdkVersion 例外,在教程中设置为 8,我将值更改为 14,因为给我带来了其他兼容性问题。

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mycompany.myfirstapp.MyActivity" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="14" />

</manifest>

Themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>


<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>
</style>
</resources>

MyActivity.java

public class MyActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // Handle presses on the action bar items
        switch (id) {
            case R.id.action_search:
                //openSearch();
                Log.i("MyActivity", "SEARCH PRESSED");
                return true;
            case R.id.action_settings:
                //openSettings();
                Log.i("MyActivity", "SETTINGS PRESSED");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>

</LinearLayout>

main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    app:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

进行此更改:

<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>


<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>
</style>

您只需更改 style.xml

替换父项

<style name="CustomActionBarTheme"   parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>

</style>

<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="colorPrimary">#AA0000</item>
</style>

AND

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">

<style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">

您评论中问题的答案

I want to know why using Theme.Holo gives error and why using the Theme.AppCompact I can't change the style of the action bar

在您正在阅读的教程中。

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher).

您正在将 支持库AppCompatActivity 一起使用,因此您必须使用 Theme.AppCompat。至于为什么您不能更改 Action Bar 的样式,这很可能是因为您没有使用本教程的正确部分。因为您正在使用支持库来自定义操作栏,所以您必须在 Styling the Action Bar tutorial.

中使用 For Android 2.1 and higher 部分

也就是res/values/themes.xml应该是

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@drawable/actionbar_background</item>
    </style>
</resources>

注意 Theme.AppCompat 的使用以及为支持库添加兼容性的行

<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>

<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_background</item>

操作栏在 Android 3.0 之前不存在。它的向后兼容性是使用支持库提供的。因此,因为您正在使用支持库来获得所需的效果,所以您需要遵循 Android 2.1(Android 3.0 之前)的所有建议。