不要求Window.FEATURE_ACTION_BAR问题 [英] Do not request Window.FEATURE_ACTION_BAR issue

查看:363
本文介绍了不要求Window.FEATURE_ACTION_BAR问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立我的应用程序,但没有成功。我尝试了几种方式,但什么也没有奏效。唯一的例外是:

I'm trying to build my app but without success.. I tried several way but nothing was worked. The exception is:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我的style.xml是:

My style.xml is:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">

        <!-- theme customizations -->

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>

        <item name="android:textColorPrimary">@color/ldrawer_color</item>
        <item name="actionMenuTextColor">@color/ldrawer_color</item>
        <item name="android:textColorSecondary">@color/ldrawer_color</item>
    </style>

</resources>

和你可以看到我已经声明

and as you can see i have declared

<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>

这是我的manifest.xml

This is my manifest.xml

 <application
        android:allowBackup="true"
        tools:replace="android:icon"
        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>

和我的 BaseActivity ,我用它来扩展其他活动

and my BaseActivity that i use to extend other activities

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public abstract class BaseActivity extends AppCompatActivity {

    public Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(BaseActivity.this.getResources().getString(R.string.app_name));
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    protected abstract int getLayoutResource();

    protected void setActionBarIcon(int iconRes) {
        toolbar.setNavigationIcon(iconRes);
    }

}

我不知道为什么它crashes..The只有这样,才能启动时不崩溃设置在style.xml父在 Theme.AppCompat.Light.NoActionBar 但以这种方式在状态栏是透明的并且不着色...

I don't know why it crashes..The only way to start the application without crash is set the parent on the style.xml in Theme.AppCompat.Light.NoActionBar but in this way the status bar is transparent and not colored...

推荐答案

使用 Theme.AppCompat.Light 告诉你想要的框架,为您提供一个动作条的Andr​​oid。但是,你正在创建自己的动作条(A 工具栏),所以你到哪里你想要的动作条来自给框架的混合信号。

Using Theme.AppCompat.Light tells Android that you want the framework to provide an ActionBar for you. However, you are creating your own ActionBar (a Toolbar), so you are giving the framework mixed signals as to where you want the ActionBar to come from.

由于您使用的工具栏,你想要 Theme.AppCompat.Light.NoActionBar

Since you are using a Toolbar, you want Theme.AppCompat.Light.NoActionBar.

接下来的步骤就是确保您的工具栏正确的风格,这似乎是你在哪里遇到了问题。要使用你的主题定义的色彩样式工具栏就像一个动作条,你需要提供一个主题,像这样:

The next step is to make sure your Toolbar is styled correctly, which seems to be where you are running into issues. To style your Toolbar like an ActionBar using the colors you defined for your theme, you need to provide a theme like so:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

请参阅有关的这个Android开发者博客文章了解详情。

这篇关于不要求Window.FEATURE_ACTION_BAR问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆