简体   繁体   中英

Styling the Action Bar in Android

How do I change the background color of the action bar when I've just created an Android project (targeted for 4.0 and up for simplicity)?

I'm a total beginner in Android and I'm stumped at the simplest things. I've read the answers to similar questions but they just don't apply, and I can't even explain why. I've read https://developer.android.com/training/basics/actionbar/styling.html and it mentions a file called res/values/themes.xml which doesn't exist when I create a project, so it's useless to me.

I know this is absolute beginner stuff, but I already want to give up. Please tell me what should I change where to have a different background color on the Android action bar. I know I need to create a custom action bar style in res/values/styles.xml but I don't know what to put in as parent of that style, everything I try gives an error.

Here is the res/values/styles.xml that is generated with a new Android project

<resources>

    <!--
        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="android:Theme.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">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

I would recommend you this page:

http://jgilfelt.github.io/android-actionbarstylegenerator/

From there you can generate one style and learn from it. I think it is the best way to learn it.

Once you download the zip file you will have 5 folders.

drawable drawable-hdpi drawable-mdpi drawable-xhdpi values

In the drawable folder, you have many xml that uses the images on the other drawable folders. Here it is defining selectors to specify if one image is for the focus status, etc...

The folder values contains one xml with colors and the other with the style. Check the style.xml to learn how you can modify something in the Action Bar.

If you just want to modify the background color:

<style name="Theme.Example" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBar.Solid.Yourapp</item>

</style>

<style name="ActionBar.Solid.Yourapp" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
    <item name="android:background">#ff0000</item> <= The color you want
</style>

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