无法正确设置Android操作栏背景

问题
当我设置ActionBar背景颜色时,条形如下所示:

我不能在这里上传图像,所以这里是文件
Android ActionBar Problem

我的值/样式文件

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="actionBarStyle">@style/ActionBarTheme</item>
</style>

<!-- Action Bar theme -->
<style name="ActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">@drawable/actionbar</item>
    <item name="background">@drawable/actionbar</item>
</style>

<style name="FullscreenTheme" parent="android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@null</item>
    <item name="MetabuttonbarStyle">@style/buttonbar</item>
    <item name="MetabuttonbarButtonStyle">@style/buttonbarButton</item>
</style>

<style name="buttonbar">
    <item name="android:paddingLeft">2dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingRight">2dp</item>
    <item name="android:paddingBottom">0dp</item>
    <item name="android:background">@android:drawable/bottom_bar</item>
</style>

<style name="buttonbarButton" />

</resources>

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.application"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/symbol"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.application.Main"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:screenorientation="portrait"
        android:theme="@style/FullscreenTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.application.Settings"
        android:parentActivityName="com.application.Main"
        android:screenorientation="portrait"
        android:theme="@style/ActionBarTheme">
        <Meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.application.Main" />
    </activity>
    <activity
        android:name="com.application.DateList"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenorientation="portrait"
        android:theme="@style/FullscreenTheme" >
    </activity>
</application>

</manifest>

我也试过这个,但同样的事情发生了

ColorDrawable draw = new ColorDrawable(R.color.grey);
getSupportActionBar().setBackgroundDrawable(draw);

我的颜色抽屉

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid
        android:color="@color/grey" />

</shape>

解决方法:

getSupportActionBar().setBackgroundDrawable(draw);

应该是,在你的情况下

getSupportActionBar().setBackgroundDrawable(getResources.getDrawable(R.drawable.your_drawable_name);

ColorDrawable的构造函数采用表示颜色的int,而不是颜色的id.使用instenad

ColorDrawable draw = new ColorDrawable(getResources().getColor(R.color.grey));

编辑:

关于style.xml,你需要带有和没有android前缀的项目:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
   <item name="android:actionBarStyle">@style/ActionBarTheme</item>  
   <item name="actionBarStyle">@style/ActionBarTheme</item>
</style>

由于minSDK值,Lint可能会抱怨它.如果是,你可以添加工具:ignore =“NewApi”到具有android:前缀的项目.例如:

 <item name="android:actionBarStyle" tools:ignore="NewApi">@style/ActionBarTheme</item>  

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...