无法正确设置Android操作栏背景
问题 当im设置ActionBar背景色时,该栏如下所示: 我无法在此上载图像,因此文件如下: 我的值/样式文件无法正确设置Android操作栏背景,android,styles,android-actionbar,Android,Styles,Android Actionbar,问题 当im设置ActionBar背景色时,该栏如下所示: 我无法在此上载图像,因此文件如下: 我的值/样式文件 <?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 them
<?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>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/grey" />
</shape>
我的颜色可绘制
<?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>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/grey" />
</shape>
对你来说,应该是
getSupportActionBar().setBackgroundDrawable(getResources.getDrawable(R.drawable.your_drawable_name);
ColorDrawable
的构造函数采用表示颜色的int,而不是颜色的id
。使用仪器
ColorDrawable draw=新的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>
我同意你的代码是可以工作的,但是在这种情况下,彩色可绘制的
为什么不能像可绘制的
一样工作呢?原因也是一样的。您应该使用getResources().getColor(R.color.grey)。我编辑了我的回答,得到的答案是“ColorDrawable的构造函数采用表示颜色的int,而不是颜色的id。”谢谢谢谢!这对我很有帮助。但是我有点困惑,为什么它不能与style.xml一起工作?哪个版本的android有你正在测试的开发?