问题
Is there any reason why is textColor
property being ignored when it is set via custom textAppearance
style?
<style name="EditTextAppearance" parent="@android:style/TextAppearance.Widget.EditText">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/blue</item> <!-- IGNORED -->
</style>
Setting style in XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
/>
For some reason, default theme control color is not overridden by this style.
The only way how I am able to set color is by setting textColor
property in EditText
(but this is not what I want):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:textColor="@color/blue"
/>
Note, custom textAppearance
with textColor
created for TextView
works without problem.
I tried to replace EditText
by android.support.v7.widget.AppCompatEditText
or android.support.design.widget.TextInputEditText
but the result was the same. Still not working. So the problem is not in EditText
implementation.
I found question with the same problem Why is textColor in android:textAppearance ignored?. Unfortunately, without answer.
回答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
.
回答2:
Still don't know why is textAppearance
ignoring textColor
when style is created as described in question, but I think I have found another way how to set textColor
via style, so somebody might find it useful.
I was playing with EditText
colors so I've created custom theme and tried to set editTextColor
property, which surprisingly worked:
<!-- consider to extend application theme over default "Base.V7.Widget.AppCompat.EditText" to achieve consistent look and feel -->
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
<item name="colorControlNormal">@color...</item> <!-- underline color -->
<item name="colorControlActivated">@color...</item> <!-- underline color on focus -->
<item name="editTextColor">@color/blue</item> <!-- ACCEPTED -->
</style>
Setting theme:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:theme="@style/EditTextTheme"
/>
At this moment you have custom "@style/EditTextAppearance"
and "@style/EditTextTheme"
. The good thing is, that you can merge "@style/EditTextAppearance"
into the "@style/EditTextTheme"
as follows:
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
...
<item name="editTextColor">@color/blue</item>
<item name="editTextStyle">@style/EditTextAppearance</item>
</style>
This way, you just have to set only theme in your EditText
:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditTextTheme"
/>
Current solution is almost perfect, but it would be better, if textColor
will be set in textAppearance style, so I've tried to remove editTextColor
from theme and replace it by textColor
property set in textAppearance style @style/EditTextAppearance
.
<style name="EditTextAppearance" parent="@android:style/TextAppearance.Widget.EditText">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/blue</item> <!-- APPLIED if appearance is part of the theme -->
</style>
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
<item name="colorControlNormal">@color...</item>
<item name="colorControlActivated">@color...</item>
<item name="editTextStyle">@style/EditTextAppearance</item>
</style>
This way textColor
contained in textAppearance
is finally applied!
来源:https://stackoverflow.com/questions/45196244/android-textcolor-ignored-in-androidtextappearance-of-the-edittext