最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android設(shè)置theme中可能遇到的坑

 更新時(shí)間:2018年06月25日 14:09:22   作者:stone_zhu  
Theme是一套UI控件和Activity的樣式,下面這篇文章主要給大家介紹了關(guān)于Android設(shè)置theme中可能遇到的坑的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

發(fā)現(xiàn)坑

最近在配置項(xiàng)目主題的時(shí)候報(bào)了如下錯(cuò)誤:

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

原因一

錯(cuò)誤寫法:

<style name="AppTheme.NoActionBar">
  <item name="android:windowActionBar">false</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

其中AppTheme使用的主題是AppCompat的主題,由于AppCompat主題下的windowActionBar和windowNoTitle的命名方式前都沒有android字樣,所以報(bào)錯(cuò)。

正確寫法:

<style name="AppTheme.NoActionBar">
  <item name="windowActionBar">false</item>
  <item name="windowNoTitle">true</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

原因二

如果主題設(shè)置成有Actionbar的Theme并且沒有配:

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

也會(huì)出這樣的錯(cuò)誤。

看下源碼:

在我們?cè)O(shè)置toolbar時(shí)候: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);點(diǎn)進(jìn)源碼可以看到源碼調(diào)用邏輯是:

public void setSupportActionBar(@Nullable Toolbar toolbar) {
  getDelegate().setSupportActionBar(toolbar);
 }

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

public void setSupportActionBar(Toolbar toolbar) {
  if (!(mOriginalWindowCallback instanceof Activity)) {
   // Only Activities support custom Action Bars
   return;
  }
  //這里會(huì)去判有沒有actionbar存在,如果有直接拋異常
  final ActionBar ab = getSupportActionBar();
  if (ab instanceof WindowDecorActionBar) {
   throw new IllegalStateException("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) {
   final ToolbarActionBar tbab = new ToolbarActionBar(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();
 }

主要在這里:

//這里會(huì)去判有沒有actionbar存在,如果有直接拋異常
final ActionBar ab = getSupportActionBar();
  if (ab instanceof WindowDecorActionBar) {
   throw new IllegalStateException("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.");
  }

好了,結(jié)束。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

会昌县| 黔西| 仪征市| 柳林县| 资中县| 子洲县| 鱼台县| 墨竹工卡县| 长兴县| 黔西县| 禄劝| 海门市| 灵石县| 正阳县| 道真| 衡东县| 双流县| 慈溪市| 东明县| 韶山市| 莱西市| 盖州市| 东光县| 松滋市| 昌宁县| 长葛市| 宜章县| 罗甸县| 阳高县| 永仁县| 西乌珠穆沁旗| 瓦房店市| 太白县| 定陶县| 宁明县| 闵行区| 贡嘎县| 镇原县| 拜泉县| 垫江县| 高阳县|