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

标签 java android android-actionbar android-theme

我正在按照官方 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 进行异常(exception)处理,在教程中设置为 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>

ma​​in_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>

最佳答案

评论中问题的答案

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。至于为什么您无法更改操作栏的样式,很可能是因为您没有使用教程的正确部分。由于您使用支持库来自定义操作栏,因此您必须使用 Styling the Action Bar tutorial 中的对于 Android 2.1 及更高版本部分。 .

那就是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>
<小时/>

Action Bar 在 Android 3.0 之前并不存在。使用支持库提供了向后兼容性。因此,由于您使用支持库来获得所需的效果,因此您需要遵循 Android 2.1(Android 3.0 之前)的所有建议。

关于java - 无法将 Theme.Holo 用于我的第一个 Android 应用程序的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32435171/

相关文章:

android - 将 ActionBar 标题对齐 Xamarin

android - setTitle 在 fragment 中不起作用

java - 如何在Java中清空缓冲区而不实际写入数据?

java - Spring数据查询实例

java - java.io.DataOutputStream 类中的 writeChar 和 writeShort 方法有什么区别?

java - Java synchronized 关键字要求的缓存刷新范围是什么?

android - 使用应用程序链接启动即时应用程序

android - 异常 : This is not supported, 使用 MenuItemCompat.getActionProvider()

java - 类型参数 T 隐藏了类型 T

android - Activity 不是从状态栏通知开始的