不要请求 Window.FEATURE_ACTION_BAR 并在主题中将 windowActionBar 设置为 false 以使用工具栏

Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead

我在将工具栏应用到我的应用程序时遇到了这个问题,当我尝试 运行 app.I 使用所有以前的 post 但没有成功时它崩溃了。 这是我的代码:

Style.xml

<style name="ToolBarStyle" parent="@style/Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>       

 </style> 

我尝试了 parent="@style/Theme.AppCompat.Light.NoActionBar" 但没有成功。

Toolbar.xmal

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ToolBarStyle" />

Manifest.axml

    <application android:label="Capzure" android:theme="@style/ToolBarStyle" android:icon="@drawable/Icon"></application>

谢谢。

您的主题应如下所示,并删除 @style/:

<style name="MyMaterialTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    ...
</style>

然后,不要在<application/>标签中定义主题。仅使用 activity 定义您想要使用 material 设计主题,即在 <activity> 标签中。

如果您的 activity 扩展了 PreferenceActivityAppCompatActivityActionBarActivity,您希望用 PreferenceFragment 开始交易,请将您的主题更改为:

<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">true</item>
    ...
</style>

并从您的工具栏中删除这一行:

android:theme="@style/ToolBarStyle"