【问题标题】:Android action bar not reflecting color changeAndroid操作栏不反映颜色变化
【发布时间】:2015-09-01 03:14:24
【问题描述】:

我已经阅读了很多关于更改操作栏颜色的答案,但我似乎无法让它发挥作用。这是我所拥有的:

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="Theme.AppCompat.Light">
        <item name="android:windowBackground">@color/actionBarBackground
        </item>
    </style>
    </resources>

我也试过

<item name="android:Background">#F33</item>

AndroidManifest.xml

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

【问题讨论】:

    标签: android-studio android-actionbar android-theme


    【解决方案1】:

    您正在更改 android:windowBackground 而不是您的 ActionBar 的背景。

    无论如何,从AppCompat v21+ 开始,您可以使用一种新的方式来自定义您的ActionBar。

    • 您的所有活动都必须扩展自 AppCompatActivity

    • 您的所有主题(需要ActionBar/Toolbar)都必须继承自 Theme.AppCompat。

    只需使用这样的主题:

    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
        <!-- Set AppCompat’s color theming attrs -->
        <item name="colorPrimary">@color/my_awesome_red</item>
        <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
    
        <!-- The rest of your attributes -->
    </style>
    

    AppCompat当前版本为23,编译项目需要API23。

    【讨论】:

    • 我的所有活动都已扩展 AppCompatActivity 您发布的代码似乎与我的代码相同,除非我遗漏了什么。我在清单本身的活动中添加了我的样式,并且在应用程序调用该活动后更改了背景,但这仅适用于活动。操作栏应该有活动吗?显然我缺少一个概念。
    • @Cliff 我不知道您是否在布局中使用了工具栏。在这种情况下,请发布您的代码。如果不是,则 actionbar 的颜色由活动使用的主题中的 colorprimary attrr 定义。在你的主题中我看不到它。
    • 在 menu_main 我有&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"&gt; &lt;item android:id="@+id/action_search" android:icon="@drawable/search_50" android:title="@string/action_search" app:showAsAction="ifRoom" /&gt; &lt;item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /&gt; &lt;/menu&gt;
    • 老实说,我不确定操作栏是在哪里创建的对我来说如何操作它的布局,我不确定最好的方法
    【解决方案2】:

    以下似乎已经成功了。我在这里找到了Change Background color of the action bar using AppCompat

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    
    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light">
        <item name="android:background">@color/actionBarBackground</item>
        <item name="background">@color/actionBarBackground</item>
    </style>
    

    但是,现在我的清单文件中的 android:label 被背景颜色覆盖了,所以我添加了

    <item name="android:displayOptions">showTitle</item>
    <item name="displayOptions">showTitle</item>
    

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多