I need to change color on my application bar to some gradient color. I created action_bar_bg.xml file in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="@color/colorPrimaryDark"
android:endColor="@color/colorPrimary"
android:startColor="@color/colorPrimary" />
</shape>
and I tried to change colorPrimary in styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@drawable/action_bar_bg</item>
</style>
but it doesn't work. I am getting error: android.content.res.Resources$NotFoundException: File res/drawable/action_bar_bg.xml from color state list resource ID #0x7f020046)
I want to achieve that result:
What do I have to change in code?
try on this way. There's a new attribute called colorPrimary which you can define in your Theme. This will give you ActionBar or Toolbar a solid color.
Following a little example:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/my_action_bar_color</item>
</style>
Please note: It has to be only colorPrimary, not android:colorPrimary, in every values-folder except the values-v21 one.
You can read more about customizing the Color Palette on Android
Do this in your onCreate in your Activity
ActionBar bar = getActionBar();
Drawable gradient = getResources().getDrawable( R.drawable.action_bar_bg);
bar.setBackgroundDrawable(gradient);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.