简体   繁体   中英

Actionbar wont change color for Theme.AppCompat.Light

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name=".MainActivity"
            android:label="@string/cb_campaign"
            android:logo="@mipmap/ic_launcher"
            android:theme="@style/MyTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

styles.xml

 <resources>
        <style name="MyTheme" parent="@android:style/Theme.AppCompat.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>

        <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
            <item name="android:background">#FFCA09</item>
        </style>
    </resources>

MainActivity.java

public class MainActivity extends AppCompatActivity{
}

截图 Edit: Original question was: How to solve Android error: java.lang.IllegalStateException:You need to use a Theme.AppCompat

Changed AndroidManifest.xml

 <style name="MyTheme" parent="@style/Theme.AppCompat.Light">

But action background color still won't change.

Just switch on the Design view, then click on AppTheme button and just change theme.

that's all.

你可以从这里学到一些材料设计cheesesquare

You need to omit the "android:" to use backwards-compatible attributes:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!--Backwards compatibility-->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="Widget.AppCompat.Light.ActionBar">
        <item name="android:background">@color/red</item>
        <!--Backwards compatibility-->
        <item name="background">@color/red</item>
    </style>
    <color name="red">#FF0000</color>
</resources>

A reference to a style resource defining a default theme for all activities in the application. Individual activities can override the default by setting their own theme attributes. For more information, see the Styles and Themes developer guide.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat">
    <activity
        android:name=".MainActivity"
        android:label="@string/cb_campaign"
        android:logo="@mipmap/ic_launcher"
        android:theme="@style/MyTheme">

android:theme="@style/Theme.AppCompat" in <application> tag u need to apply your theme. one suggestion its better to go with material design stackoverflow link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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