今天就跟大家聊聊有關(guān)如何在Android中設(shè)置theme,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
原因一
錯(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."); }Android是什么
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容,你們對(duì)如何在Android中設(shè)置theme有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁(yè)名稱:如何在Android中設(shè)置theme-創(chuàng)新互聯(lián)
文章出自:http://www.yijiale78.com/article44/ddoiee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、用戶體驗(yàn)、Google、搜索引擎優(yōu)化、網(wǎng)站營(yíng)銷、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容