Change Android Action Bar Color: Guide
There are two ways to change the color of the Android top bar: by modifying the theme or dynamically changing it in the code.
- Define a new theme in the res/values/styles.xml file as shown below:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item> // 修改顶部栏的颜色
</style>
Then define the color values in the res/values/colors.xml file as shown below:
<color name="colorPrimary">#ff0000</color> // 设置顶部栏颜色为红色
Finally, apply the theme to the corresponding Activity in the AndroidManifest.xml file, as shown below:
<activity android:name=".MainActivity"
android:theme="@style/AppTheme">
</activity>
- You can dynamically modify the color of the top bar in an Activity by using the following code.
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
colorPrimary is a color value defined in colors.xml.
The color of the Android top bar can be changed by using either of the above methods.