我发现了类似的问题,但是答案没有用。我不想更改colorPrimary

ActionBar不是ToolBar,因此在 Activity 的布局XML中不存在。以下命令无效,并且bakground仍设置为colorPrimary

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:statusBarColor">@color/colorBlack</item>
        <item name="android:colorBackground">@color/colorBlack</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:backgroundStacked">@color/colorBlack</item>
        <item name="android:backgroundSplit">@color/colorBlack</item>
        <item name="android:background">@color/colorBlack</item>
    </style>
</resources>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

最佳答案

以编程方式您可以做

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#YOUR_COLOR")));

10-07 20:54