当前位置: 代码迷 >> Android >> 【Android开发心得】This Activity already has an action bar supplied by the window decor
  详细解决方案

【Android开发心得】This Activity already has an action bar supplied by the window decor

热度:1267   发布时间:2016-04-27 23:29:52.0
【Android开发经验】This Activity already has an action bar supplied by the window decor

问题描述:继承自AppCompatActivity,使用Toolbar替代ActionBar的时候,出现错误

错误信息:

  • 2.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.

问题原因描述:由于Activity已存在ActionBar,所以使用Toolbar进行替换时出错

解决思路:想办法去掉ActionBar

解决方案

  • 1.使用Theme去掉ActionBar。使用Theme.AppCompat.Light.NoActionBar或者是Theme.AppCompat.NoActionBar主题,即可去掉ActionBar,即可解决此问题。
    代码如下
<resources>    <style name="AppTheme" parent="AppTheme.Base">    </style>    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">        <item name="colorPrimary">@color/accent_material_light</item>        <item name="colorPrimaryDark">@color/accent_material_light</item>        <item name="android:windowBackground">@color/dark</item>    </style></resources>
  • 2.若不能使用以上方案,则设置Theme的属性解决此问题。

在项目中的所有values-xx文件夹中的styles.xml中添加下面代码,从而去掉ActionBar

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

有一个细节需要注意,因为这几个属性对版本的要求不同,所以如果某个属性在你的App版本下不能识别,删除保留其他即可。

转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案