How to change the background color i.e. “element c

2019-08-21 07:06发布

问题:

Please help...

I have to change the element color and text color of status bar and I also have to change the text color of title bar in Android:

The existing code is as follows:

styles.xml

<style name="AppThemeNew" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:statusBarColor">@color/status_bar_color</item>
        <item name="android:windowLightStatusBar">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#FDFEFE</color>
    <color name="colorPrimaryDark">#F4F6F6</color>
    <color name="colorAccent">#088da5</color>
    <color name="status_bar_color">#F4F6F6</color>
    <color name="status_bar_element_color">#AAB7B8</color>
</resources>

row_data.xml

Layout

Please help.....

回答1:

To change statusbar' element and text color, you could create custom theme like this:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:statusBarColor">@color/status_bar_color</item>
        <item name="android:windowLightStatusBar">false</item>
    </style>

Notes:

  1. status_bar_color would be your custom color resource.

  2. android:windowLightStatusBar = true, status bar text color will be compatible (grey) when status bar color is light.

    android:windowLightStatusBar = false, status bar text color will be compatible (white) when status bar color is dark.

    ref is here

  3. Tested in above api level 23



回答2:

The action bar takes the colour for the titles and icons from textColorPrimary

However this will also affect the default text for edit texts and text views if you set it on the app theme.

To get around this create a separate theme for the app bar that includes the text colour change and either reference it in the app theme as the app bar style or set it on the toolbar in the XML if you have a custom toolbar.