将ActionBar背景颜色设置为XML

时间:2018-03-19 11:20:26

标签: android android-actionbar

我发现了类似的问题,但答案没有用。我不想更改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>

1 个答案:

答案 0 :(得分:0)

以编程方式执行

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