更改ActionBar背景的问题 - Android

Dav*_*iga 2 android

嘿伙计我是Android开发的新手,我正在努力改变我的ActionBar的背景.在我的themes.xml文件中添加以下内容时res->values

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
   <item name="android:background">@drawable/actionbar_background</item> 
</style> 
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

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

我不是肯定为什么我收到这个消息并且可以用一只手.

大卫

ssa*_*tos 12

只是猜测,但我的赌注是你没有任何可绘制的名字 actionbar_background.png

仔细检查png资源中是否存在拼写错误.它应该在任何drawable文件夹中.或者你可以设置一个颜色.-

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
   <item name="android:background">#FF0000</item> 
</style> 
Run Code Online (Sandbox Code Playgroud)

请注意,硬编码值是一种不好的做法.如果您最终选择背景颜色,则应考虑在单独的资源文件(即colors.xml)中为该颜色创建资源项

<color name="red">#FF0000</color>
Run Code Online (Sandbox Code Playgroud)

并在您的项目样式中引用它 @color/red


Mel*_*cuk 5

您可以在创建活动时使用它.(的onCreate)

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