简体   繁体   中英

Issue with changing ActionBar background - Android

Hey guys I am newer to Android development and I am working on changing my ActionBar's background. When adding the following in my themes.xml file within res->values

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
   <item name="android:background">@drawable/actionbar_background</item> 
</style> 

I receive this error message:

error: Error: No resource found that matches the given name (at 'android:background' with value '@drawable/
 actionbar_background').

I am not positive why I am receiving this message and could use a hand.

David

Just guessing, but my bet is that you don't have any drawable named actionbar_background.png

Double check for possible typos in your png resource. It should be in any drawable folder. Or you could just set a color with.-

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
   <item name="android:background">#FF0000</item> 
</style> 

Please notice that hardcoding values is a bad practice. If you finally go for a background color, you should consider creating a resource item for that color, in a separate resource file (ie colors.xml)

<color name="red">#FF0000</color>

And reference it with in your item style with @color/red

You can use it when activity created. (onCreate)

ActionBar bar = getActionBar();
//for color
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00C4CD")));
//for image
bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.settings_icon));

You are missing a reference file in drawable folder. you will find the folder under res just put an image file under drawable named as actionbar_background and the error should be resolved. you can download the image just google actionbar_background. hope it helps

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM