Why is textColor in android:textAppearance ignored

2019-08-01 19:18发布

问题:

I use android support library, which version I don not know because I do not know of a way how to check it. The problem I am facing that apparently the value for android:textColor attribute is ignored. If I define a style and assign it to textAppearance of EditText the color textColor is ignored.

So, I have a following layout:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- This text should be red, but it is black -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text color via TextAppearance"
        android:textAppearance="@style/TextAppearance.EditText" />

    <!-- The hint should be green, but it is grey -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint color via TextAppearance"
        android:textAppearance="@style/TextAppearance.EditText" />

     <!-- Text is red as set in android:textColor -->
     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text color via android:textColor"
        android:textColor="#880000"
        android:textSize="18sp" />

    <!-- Hint is green as set in android:textColorHint -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint color via android:textColorHint"
        android:textColorHint="#008800"
        android:textSize="18sp" />

</LinearLayout>

I have defined the following style:

<!-- Text Appearance -->
<style name="TextAppearance.EditText" parent="">
    <item name="android:textColor">#880000</item>      <!-- this color is ignored -->
    <item name="android:textColorHint">#008800</item>  <!-- this color is ignored -->
    <item name="android:textSize">18sp</item>
</style>

Is this a bug in support library or did I miss something?

回答1:

Somewhere along the line, the wrong/default value for textColor is being picked up and applied. You can force the android:textColor that you define in android:textAppearance to be used by setting android:textColor="@null" in your XML for EditText. This should work for android:textColorHint as well.

(This reflects the accepted answer to this post.)



回答2:

Use

style="@style/TextAppearance.EditText"

instead of

android:textAppearance="@style/TextAppearance.EditText"

It should work!