简体   繁体   English

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

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

I'm developing my first Android app following the official Android tutorial, so I'm new, but when I try to styling the action bar with the Theme.Holo my app crash down during the starting, into the log I've the following message: 我正在按照官方Android教程开发我的第一个Android应用程序,所以我是新手,但是当我尝试使用Theme.Holo样式化操作栏时, Theme.Holo我的应用程序在启动过程中崩溃了,进入日志后,我有以下内容信息:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

I know that if I use the Theme.AppCompact it's working, but in the tutorial does not mention problems like this one with the Theme.Holo, and does not give a solution, because if I use the Theme.AppCompact the app works, but I can't modify the style of the action bar with a custom color or with a background image. 我知道如果我使用Theme.AppCompact可以正常工作,但是在本教程中并未提及Theme.AppCompact这样的问题,并且没有给出解决方案,因为如果我使用Theme.AppCompact,则该应用程序可以运行,但是我无法使用自定义颜色或背景图片来修改操作栏的样式。

I've followed exactly the tutorial until this point: https://developer.android.com/training/basics/actionbar/styling.html make an exception for the minSdkVersion, in the tutorial is set to 8, I've changed the value to 14 because gives me other compatibility problems. 到目前为止,我一直严格按照本教程进行操作: https : //developer.android.com/training/basics/actionbar/styling.html为minSdkVersion设置例外,在本教程中设置为8,我更改了值14,因为它给了我其他兼容性问题。

This is the AndroidManifest.xml 这是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 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 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 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 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 styles.xml

<resources>

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

</resources>

The answers to the questions in your comment 您评论中问题的答案

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 我想知道为什么使用Theme.Holo会出错,为什么使用Theme.AppCompact我无法更改操作栏的样式

are in the tutorial you are reading. 在您正在阅读的教程中。

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). 注意:如果使用操作栏的支持库API,则必须使用(或覆盖)Theme.AppCompat系列样式(而不是API级别11及更高版本中的Theme.Holo系列)。

You are using the Support Library with AppCompatActivity hence you must use Theme.AppCompat . 您正在将支持库AppCompatActivity一起使用,因此必须使用Theme.AppCompat As to why you can't change the style of the Action Bar that is most likely because you are not using the correct section of the tutorial. 至于为什么您不能更改操作栏样式的原因很可能是因为您没有使用本教程的正确部分。 Because you are using the Support Library to customise the Action Bar you must use the section For Android 2.1 and higher in the Styling the Action Bar tutorial . 由于您正在使用支持库来自定义操作栏,因此必须使用“ 设置操作栏样式”教程中的“ Android 2.1及更高版本 ”部分。

That is res/values/themes.xml should be 那是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>

Note the use of Theme.AppCompat and the lines which add compatibility for the Support Library 请注意Theme.AppCompat的使用以及增加了对支持库的兼容性的行

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

and

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

The Action Bar did not exist pre Android 3.0. Android 3.0之前的操作栏不存在。 Backwards compatibility for it is provided using the Support Library. 使用支持库提供了向后兼容性。 Hence, because you are using the Support Library to get the desired effects you need to follow all of the advice for Android 2.1 (pre Android 3.0). 因此,由于您正在使用支持库来获得所需的效果,因此您需要遵循Android 2.1(Android 3.0之前的版本)的所有建议。

make this change: 进行更改:

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

you have to just change in style.xml 您只需要更改style.xml

Replace Parent 取代父母

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

</style>

to

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

to

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

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

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