简体   繁体   English

设置Android ActionBar的背景颜色

[英]Setting background color of android actionbar

I am operating with the compatibility library v7. 我正在使用兼容性库v7。

I am just trying to set the color of my actionbar (for Android 2.1 and above - though I run Android 4.4.2) to a solid color. 我只是想将操作栏的颜色(适用于Android 2.1及更高版本-尽管我运行Android 4.4.2)设置为纯色。

However the color does not change. 但是颜色不会改变。 It remains the same. 它保持不变。

I have also tried creating a solid drawable with the color but that also does not change. 我也尝试过用颜色创建一个可绘制的实体,但是它也不会改变。

Finally I tested if I could change the backgroudn of my layout and I could - it must be something about the actionbar background which I'm not getting. 最后,我测试了是否可以更改布局的背景,并且可以-它一定是关于我没有得到的操作栏背景的内容。

Here is the code: 这是代码:

    <?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">

         <item name="android:background">#0000ff</item>  

    </style>
</resources>

This seems to work for me. 这似乎为我工作。 Try to use a resource instead of a raw value. 尝试使用资源而不是原始值。

<style name="app_theme" parent="@android:style/Theme.Holo">
    <item name="android:actionBarStyle">@style/app_action_bar</item>
</style>

<style name="app_action_bar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@color/google_lightest_gray</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:windowActionBarOverlay">false</item>
</style>

use getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#808080"))); 使用getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#808080"))); in Activity extends SherlockActivity or the color as your wish :) Activity extends SherlockActivity或根据需要选择颜色:)

您可以使用:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.colorname));

use the following code getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg_color.xml)); 使用以下代码getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg_color.xml));

actionbar_bg_color.xml actionbar_bg_color.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
      <solid android:color="@color/actionbar_bg"/>  <!-- set desired #0000ff color in color.xml
 you can use here solid color as well as gradient -->
 </shape>

hope this help you 希望这对你有帮助

I solved it by: 我通过以下方法解决了它:

  • Properly installing the support-v7-appcompat library.My mistake. 正确安装了support-v7-appcompat库。我的错。
  • Shifting the styling to the res/values/styles.xml files for each version, ie one for backwards compatibility and one for values-v11. 对于每个版本,将样式转移到res / values / styles.xml文件,即,一个用于向后兼容,一个用于values-v11。

I don't know why I couldnt get the result I wanted within themes.xml. 我不知道为什么不能在themes.xml中得到想要的结果。

If anyone has an answer I'd be grateful 如果有人有答案,我将不胜感激

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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