簡體   English   中英

Android:設置操作欄背景顏色

[英]Android: Setting Action Bar Background Color

我知道有很多關於這方面的問題,但我無法弄清楚我的風格有什么問題。

這是我的清單:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

在values \\ styles.xml中,我有(編輯,根據下面的答案,我將顏色移動到資源)。

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
  <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- ActionBar styles -->
  <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@color/white</item>
</style>
</resources>

並添加了res / values / color.xml文件,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
</resources>

如果我將第一個樣式的行更改為:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

這確實反映並使條形變暗。

為什么我的操作欄沒有設置為白色?

編輯1:如果有幫助,我的minSdkVersion為16,而我的targetSdkVersion為21。

<!-- ActionBar styles -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#FFFFFF</item> 
</style>

android:background屬性只接受參考值,你插入原始顏色值,所以它被忽略。 改為創建顏色資源並指向該資源,如<item name="android:background">@color/white</item>

我認為這就是你需要的:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/app_color_Primary</item>
    <item name="colorPrimaryDark">@color/app_color_primary_dark</item>
    <item name="colorAccent">@color/app_color_accent</item>
    ...
</style>

您也可以動態設置ActionBar的背景顏色。 例:

final ActionBar actionBar = getActionBar();
final int actionBarColor = getResources().getColor(R.color.green);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));

注意:我已經在values /中創建了一個名為color.xml的新文件,並為標記'green'定義了一個顏色值,如:

<color name="green">#8CBE41</color>

希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM