Android工具栏无法正常工作

时间:2015-06-21 20:06:47

标签: android android-actionbar material-design android-toolbar

我正在尝试在Android中创建一个向后兼容的工具栏,我按照多个样式指南中提供的所有建议来尝试完成此操作。但是,它似乎仍然不起作用。这是风格:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <color name="primary">#da291c</color>
    <color name="primary_dark">#9d1e14</color>
    <color name="white">#ffffff</color>

</resources>

这是工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary" />

这是onCreate方法的主要活动:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

不太确定是什么问题,因为我正在遵循许多来源提供的所有指示。这是日志:

java.lang.IllegalArgumentException: AppCompat does not support the current theme features

我一直在看这段代码,并且在无数教程和StackOverflow问题上,几个小时,但无济于事。如果有人可以帮助我,我将不胜感激。在此先感谢:)

编辑1: 这是我的清单文件:

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

将您的样式更改为:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

或继承Theme.AppCompat.Light.NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
</style>

参考:Chris Banes&#39;发表here

相关问题