国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-21 21:49:40
字體:
供稿:網(wǎng)友

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

最近在配置項目主題的時候報了如下錯誤:

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

原因一

錯誤寫法:

<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字樣,所以報錯。

正確寫法:

<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>

也會出這樣的錯誤。

看下源碼:

在我們設(shè)置toolbar時候: ((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;  }  //這里會去判有沒有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(); }

主要在這里:

//這里會去判有沒有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é)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜都市| 定结县| 台东市| 南宁市| 沅陵县| 皋兰县| 顺昌县| 乐东| 绩溪县| 兴宁市| 久治县| 修水县| 石景山区| 灌阳县| 英超| 高密市| 阳东县| 扶沟县| 南丰县| 永顺县| 铜山县| 绍兴市| 潞西市| 邯郸市| 鄂托克前旗| 德州市| 会昌县| 长顺县| 邛崃市| 松阳县| 莱芜市| 南溪县| 泾阳县| 于田县| 土默特右旗| 惠州市| 多伦县| 根河市| 汝城县| 南投县| 罗甸县|