"此活动已经有一个由窗口装饰提供的操作栏..."错误

时间:2017-02-26 20:43:56

标签: java android xml

所以我试图制作一个使用自定义工具栏(带标签)的活动但我收到此错误This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

这是我的活动课程:

import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;

public class mainScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    Toolbar toolBarObject = (Toolbar)findViewById(R.id.mainScreen_ToolBar);
    toolBarObject.setTitle("Note B");
    setSupportActionBar(toolBarObject);

    AppBarLayout appBarObject = (AppBarLayout)findViewById(R.id.mainScreen_AppBar);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainScreen_Parent"
android:theme="@style/AppTheme.NoActionBar">

<android.support.design.widget.AppBarLayout
    android:id="@+id/mainScreen_AppBar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="?attr/actionBarTheme"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/mainScreen_ToolBar"
        android:layout_weight="1" />

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mainScreen_TabsParent"
        app:tabIndicatorColor="@android:color/white"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextAppearance="@android:style/TextAppearance.Material.Widget.TabWidget"
        android:background="@color/colorPrimary">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subjects"
            tools:ignore="HardcodedText"
            android:id="@+id/MainScreen_Subjects" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pinned"
            tools:ignore="HardcodedText"
            android:id="@+id/MainScreen_Pinned" />

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

主题:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBarOverlay">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:colorForeground">@android:color/background_light</item>
    <item name="actionMenuTextColor">@android:color/black</item>
</style>

我尝试了互联网上的解决方案,例如将windowActionBar设置为false并添加windowNoTitle属性,但这些属性都不起作用

1 个答案:

答案 0 :(得分:1)

NoActionbar主题应如下所示:

 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

在您的清单中,将您的活动主题设置为“AppTheme.NoActionBar”

 <activity
        android:name=".mainScreen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />

为您的应用添加另一种样式:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

在Manifest中设置应用程序主题:

<application
        ....
        android:theme="@style/AppTheme">