Android 第十九天 自定義ActionBar遇到問題

問題一:
在Android開發(fā)中,使用ToolBar控件替代ActionBar控件,需要在java代碼中使用setSupportActionBar()方法,如下:

Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolBar);
this.setSupportActionBar(toolbar);
原因:方法參數報錯

這種報錯是因為導錯了類,把以下代碼

import android.widget.Toolbar;
更換成以下代碼

import android.support.v7.widget.Toolbar;
修改ok

問題二:
carsh報如下錯誤:Unable to start activity ComponentInfo{com.xiaoyan.wether/com.xiaoyan.wether.MainActivity}: java.lang.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.

最終找到原因:
在AndoridMainfest.xml中如下設置:
android:theme="@style/AppTheme"
然后sytle中AppTheme如下設置:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

其中parent="Theme.AppCompat.Light.DarkActionBar"設置為parent="Theme.AppCompat.Light.NoActionBar"即可。
ok。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容