Edittext color is white in appcompat 22.2

2019-02-15 11:00发布

问题:

EDIT 2 : This is happening because of the line

<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>

in styles.xml. I'm using this to display light color (white) texts and back home button in ToolBar. If i change this to <item name="theme">@style/ThemeOverlay.AppCompat.Light</item> then EditText is working as expected but my ToolBar text and back home button colors are changes to dark (black).

As I'm using dark colored ToolBar I would like to show the text and back home button in light (white) color.

I tried using <item name="android:theme">...</item> instead of <item name="theme">...</item> But nothing helped me, The toolbar text and back button still remains dark color! Any one figured out? Help me!

ACTUAL QUESTION :

In an appcompat-v7:22.2.0 I used an EditText. An I'm using following theme for an Activity

<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/myPrimaryColor</item>
    <item name="colorPrimaryDark">@color/myPrimaryDarkColor</item>
    <item name="colorAccent">@color/myAccentColor</item>
    <item name="android:windowBackground">@color/myWindowBackground</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="colorControlHighlight">#0000AA</item>
</style>

But the color of an EditText is coming in white color. Bottom line, hint and text all are in white. I heard there is a bug in AppCompat But how to resolve this?

EDIT 1 : Adding more information

XML file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="@dimen/marintopforelements"
          android:gravity="center"
          android:orientation="vertical"
          android:padding="10dp"
          android:paddingTop="@dimen/templatePaddingTop">

          <android.support.v7.widget.AppCompatEditText
                android:id="@+id/emailresponse"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center"
                android:layout_marginLeft="@dimen/examtextdiff"
                android:imeOptions="actionNext"
                android:inputType="textEmailAddress"
                android:minLines="2"
                android:paddingRight="5dp"
                android:textSize="@dimen/inboxTabTextSize"/>

</LinearLayout>

colors.xml

<color name="myPrimaryColor">#3F51B5</color>
<color name="myPrimaryDarkColor">#FF304081</color>
<color name="myAccentColor">#3F51B5</color>

So previously when I was using AppCompat-V7:21 It was coming like below image

But after upgrading to AppCompat-V7:22.2.0 Its like below image

So if it focused its coming like below image

Only that cursor line is in given accent color. But the hint and text are always in white! And this is happening even in both EditText and AppCompatEditText Hope this helps

回答1:

I'd recommend you to create a theme/style only for your EditText. Appcompatuses by default the android:textColorSecondary as the color for the bottom line of the EditText though you haven't defined this in your Theme so it's hard to tell why your bottom line, text and hint is colored in white.

Here's an example for a separate Theme for EditText's

<style name="ThemeEditTextLight">
    <!-- bottom line color -->
    <item name="colorControlNormal">#000000</item>
    <!-- text color -->
    <item name="android:textColor">#000000</item>
    <!-- hint text color -->
    <item name="android:textColorHint">#BDBDBD</item>
</style>

And then simply set the Theme to your EditText:

 <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="My hint"
     android:theme="@style/ThemeEditTextLight" />

This approach gives you much more control over the coloring of your EditTexts.



回答2:

Okie, I fixed it!

What I was doing was, I want to display ToolBar content in white color as my ToolBar is dark so I used this line

<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> in styles.xml

This was working fine with appcompat-v7:21.+.

But in appcompat-v7:22.2.0 as the app:theme was depreciated (mentioned by @Guillaume Imbert in his answer).

So here was the confusion. I didn't use 'app:theme` any where in my project but it still keep showing me warning. So again I googled and found the this link.

Which leads me to following blog by Chris Banes clearly explained about it.

Now I removed <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> from styles.xml. So my ToolBar content is dark (Black).

Finally adding android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" in my ToolBar did the trick, So my toolbar code looks like

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/action_bar_size_x2"
    android:background="@color/myPrimaryColor"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

So this may help someone.

Thanks for giving me the good lead, both @reVerse and @Guillaume Imbert. Without your answers I would not able to fix this. Thanks a Lot :)



回答3:

First, you should use EditText, and not the compat one. At runtime, the support library replaces the EditText with the appcompat one only if needed.

Regarding your problem, appcompat 22.2 introduces the use of android:theme instead of app:theme. I think the item theme in your style was not applied with the previous version of appcompat, and it is now.

By the way, I don't know if using the theme item into a theme is a good practice.