问题描述:

I am trying to customize the ActionBar background color to white. Using the code actionBar.setBackgroundDrawable(whiteclor....); works without problem. When I try using style, it seems not working on android 4.2. Any idea?

styles.xml under values,

<!-- 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>

style.xml under v14

<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>

网友答案:

I think its because the parent of your custom actionbar is not deriving from AppCompat, Try changing

<style name="MyTheme.ActionBar" parent="android:style/Widget.Holo.Light.ActionBar.TabBar">

to

<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>
相关阅读:
Top