美文网首页
2018-10-14【Android设置theme中遇到的坑】

2018-10-14【Android设置theme中遇到的坑】

作者: mahongyin | 来源:发表于2018-10-14 17:22 被阅读56次

最近在配置项目主题的时候报了如下错误:

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR

你应该使用AppCompat的主题等提示

1.

错误写法:

<stylename="AppTheme.NoActionBar">

  <itemname="android:windowActionBar">false</item>

  <itemname="android:windowNoTitle">true</item>

  <itemname="android:windowDrawsSystemBarBackgrounds">true</item>

  <itemname="android:statusBarColor">@android:color/transparent</item>

 </style>

这是由于你的Activity  和AppCompatActivity的不同

正确写法:

<stylename="AppTheme.NoActionBar">

  <itemname="windowActionBar">false</item>

  <itemname="windowNoTitle">true</item>

  <itemname="android:windowDrawsSystemBarBackgrounds">true</item>

  <itemname="android:statusBarColor">@android:color/transparent</item>

 </style>

其中AppTheme使用的主题是AppCompat的主题,由于AppCompat主题下的windowActionBar和windowNoTitle的命名方式前都没有android字样,所以报错。

2.

如果主题设置成有Actionbar的Theme并且没有配:

<item name="windowActionBar">false</item>

<item name="windowNoTitle">true</item>

也会出这样的错误。

看下源码:

在我们设置toolbar时候: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);点进源码可以看到源码调用逻辑是:

publicvoidsetSupportActionBar(@NullableToolbar toolbar) {

  getDelegate().setSupportActionBar(toolbar);

 }

在往下追一步,出真相了:

publicvoidsetSupportActionBar(Toolbar toolbar) {

  if(!(mOriginalWindowCallback instanceofActivity)) {

   // Only Activities support custom Action Bars

   return;

  }

  //这里会去判有没有actionbar存在,如果有直接抛异常

  finalActionBar ab = getSupportActionBar();

  if(ab instanceofWindowDecorActionBar) {

   thrownewIllegalStateException("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.");

  }

  // If we reach here then we're setting a new action bar

  // First clear out the MenuInflater to make sure that it is valid for the new Action Bar

  mMenuInflater = null;

  // If we have an action bar currently, destroy it

  if(ab != null) {

   ab.onDestroy();

  }

  if(toolbar != null) {

   finalToolbarActionBar tbab = newToolbarActionBar(toolbar,

     ((Activity) mContext).getTitle(), mAppCompatWindowCallback);

   mActionBar = tbab;

   mWindow.setCallback(tbab.getWrappedWindowCallback());

  } else{

   mActionBar = null;

   // Re-set the original window callback since we may have already set a Toolbar wrapper

   mWindow.setCallback(mAppCompatWindowCallback);

  }

  invalidateOptionsMenu();

 }

主要在这里:

//这里会去判有没有actionbar存在,如果有直接抛异常

finalActionBar ab = getSupportActionBar();

  if(ab instanceofWindowDecorActionBar) {

   thrownewIllegalStateException("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.");

  }

相关文章

网友评论

      本文标题:2018-10-14【Android设置theme中遇到的坑】

      本文链接:https://www.haomeiwen.com/subject/fzqtzftx.html