将 ActionBar 区域的颜色更改为绿色

Changing the color of ActionBar area to green

我是 Android ActionBar 的新手。目前我正在尝试在 ActionBar 区域创建一个菜单,但它总是以黑色出现。任何人都可以帮我更改 ActionBar 的背景颜色吗? 附上我的作品请大家看看,

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="Theme.AppCompat">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

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

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ThemeUtil.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_main);

}

color.xml

    <color name="actionbar_background">#008080</color> 

manifest.xml

    android:theme="@style/CustomActionBarTheme" >

ActionBarActivity 已弃用,您应该使用 AppCompatActivity.

如果您的清单文件中 CustomActionBarTheme 设置为 android:theme,则无需使用 ThemeUtil。将其从您的 MainActivity 中删除。然后,重写你的 styles.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme" parent="Theme.AppCompat">
        <item name="colorPrimaryDark">/*This will tint the status bar on lollipop devices*/</item>
        <item name="colorPrimary">@color/actionbar_background</item>
    </style>
</resources>

只要您的样式扩展了 Theme.AppCompat.*,那么 colorPrimary 会自动设置 ActionBar 颜色。在 Lollipop 设备上,colorPrimaryDark 设置状态栏颜色。 accent 设置一些小部件项目的颜色。