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

20

当我将工具栏应用到我的应用程序中时,遇到了这个问题,并且在尝试运行应用程序时崩溃了。我使用了之前的所有帖子,但没有运气。

这是我的代码:

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>

谢谢。

1个回答

23

你的主题应该像这样,同时删除 @style/

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

那么,在<application/>标签中不要定义主题。只在您想使用材料设计主题的活动中定义它,即在<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" 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接