欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

相關(guān)文章

  • Flutter實(shí)現(xiàn)打印功能的示例詳解

    Flutter實(shí)現(xiàn)打印功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何通過?Flutter?實(shí)現(xiàn)調(diào)用打印機(jī)打印的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Flutter 中 Dart的Mixin示例詳解

    Flutter 中 Dart的Mixin示例詳解

    這篇文章主要介紹了Flutter 中 Dart的Mixin的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Android 混淆代碼詳解及實(shí)例

    Android 混淆代碼詳解及實(shí)例

    本文主要介紹Android 混淆代碼的資料,這里整理了詳細(xì)資料及代碼實(shí)例,有需要做Android 混淆代碼的朋友可以參考下
    2016-09-09
  • 利用Jetpack Compose繪制可愛的天氣動(dòng)畫

    利用Jetpack Compose繪制可愛的天氣動(dòng)畫

    Jetpack Compose是用于構(gòu)建原生Android UI的現(xiàn)代工具包。Jetpack Compose使用更少的代碼,強(qiáng)大的工具和直觀的Kotlin API,簡化并加速了Android上的UI開發(fā)。本文將利用Jetpack Compose繪制可愛的天氣動(dòng)畫,感興趣的可以了解一下
    2022-01-01
  • 深入理解Kotlin的泛型系統(tǒng)

    深入理解Kotlin的泛型系統(tǒng)

    Kotlin 泛型即 “參數(shù)化類型”,將類型參數(shù)化,可以用在類,接口,方法上。下面 這篇文章主要給大家介紹了關(guān)于Kotlin泛型系統(tǒng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • Android水波紋載入控件CircleWaterWaveView使用詳解

    Android水波紋載入控件CircleWaterWaveView使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android水波紋載入控件CircleWaterWaveView使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android中使用GridLayout網(wǎng)格布局來制作簡單的計(jì)算器App

    Android中使用GridLayout網(wǎng)格布局來制作簡單的計(jì)算器App

    這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來制作簡單的計(jì)算器App的實(shí)例,GridLayout比表格布局TabelLayout更容易用來制作計(jì)算器這樣的多按鈕排列的界面,需要的朋友可以參考下
    2016-04-04
  • Flutter?頁面跳轉(zhuǎn)和傳值的實(shí)現(xiàn)

    Flutter?頁面跳轉(zhuǎn)和傳值的實(shí)現(xiàn)

    跳轉(zhuǎn)傳值是再普通不過的小功能了,在開發(fā)中會(huì)經(jīng)常用到,比如列表進(jìn)入詳情,本文主要介紹了Flutter?頁面跳轉(zhuǎn)和傳值的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • Android Activity的生命周期與加載模式超詳細(xì)圖文解析

    Android Activity的生命周期與加載模式超詳細(xì)圖文解析

    這篇文章主要介紹了Android Activity的生命周期與加載模式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • android讀寫cookie的方法示例

    android讀寫cookie的方法示例

    這篇文章主要介紹了android讀寫cookie的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05

最新評(píng)論