[英]Android: How to change my action bar background color
我使用Android Studio創建了一個使用ListView和導航抽屜模板進行活動的項目,我的目標是API 14+,並且正在Galaxy Note 3 Kitkat上進行測試-以下代碼的結果是操作欄“無”顏色是黑色(深灰色),而不是我想要的藍色。
在build.gradle文件中
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'}
在AndroidManifest.xml中
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
在Styles.xml中(我有AppTheme)
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@drawable/blue_action_bar_color</item>
</style>
在blue_action_bar_color.xml中
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/action_bar"/>
</shape>
在colors.xml中
<color name="action_bar">#3b5b98</color>
那我在這里想念什么?
編輯1:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
編輯2:
我不能用
<style name="AppTheme" parent="android:Theme.Holo.Light">
當我的應用程序崩潰並顯示錯誤消息:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
問題是您要擴展AppCompat
包隨附的ActionBarActivity
並對其實現Widget.Holo.Light.ActionBar
樣式。 很明顯,您會收到該錯誤。 因此,要做一件事就是稍微更新一下您的樣式,如下所示:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimaryDark">@color/primaryDarkColorActionBar</item>
<item name="colorPrimary">@color/primaryColorActionBar</item>
</style>
另外,在最新的appcompat更新(即22.1.1
)中不推薦使用ActionBarActivity
。 因此,最好改用AppCompatActivity
。
實際上只是更改:
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@drawable/blue_action_bar_color</item>
</style>
成為:
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@drawable/blue_action_bar_color</item>
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.