如何在Android中設置theme-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關如何在Android中設置theme,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設、域名、虛擬主機、成都網(wǎng)站托管、企業(yè)網(wǎng)站設計、加查網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

原因一

錯誤寫法:

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

原因二

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

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

也會出這樣的錯誤。

看下源碼:

在我們設置toolbar時候: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);點進源碼可以看到源碼調用邏輯是:

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.");
  }
Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領導及開發(fā)。

看完上述內容,你們對如何在Android中設置theme有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁名稱:如何在Android中設置theme-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article44/ddoiee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、用戶體驗Google、搜索引擎優(yōu)化網(wǎng)站營銷、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設