我正在尝试将ActionBar背景颜色自定义为白色。使用代码actionBar.setBackgroundDrawable(whiteclor....);
可以正常工作。当我尝试使用样式时,它似乎无法在android 4.2上运行。任何的想法?
styles.xml下的styles.xml,
<!-- customized theme -->
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/MyTheme.ActionBar</item>
<item name="android:actionBarTabBarStyle">@style/MyTheme.ActionBar.TabBar</item>
<item name="android:actionBarTabStyle">@style/MyTheme.ActionBar.TabView</item>
<item name="android:actionBarTabTextStyle">@style/MyTheme.ActionBar.TabText</item>
</style>
<style name="MyTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar.TabBar">
<item name="android:background">#ffffff</item>
</style>
v14下的style.xml
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
我认为是因为您的自定义操作栏的父级不是从AppCompat派生的,请尝试更改
<style name="MyTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar.TabBar">
到
<style name="MyTheme.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:displayOptions"></item>
<item name="android:background">#ffffff</item>
<!-- Support library compatibility -->
<item name="background">#ffffff</item>
</style>