开发者

textColor not applied when used as them

开发者 https://www.devze.com 2023-02-25 09:40 出处:网络
I have a style that includes textColor, textSize, textStyle and typeface.When applied directly to an EditText widget, the color of the text is as specified (as well as the other attributes), but when

I have a style that includes textColor, textSize, textStyle and typeface. When applied directly to an EditText widget, the color of the text is as specified (as well as the other attributes), but when applied as a theme to the activity or the entire application, the size is fine but the color is not applied. What I am missing?

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="fap" parent="android:Theme.Holo">
      <item name="android:textSize">24sp</item>
      <item name="android:textColor">#FF0000</item>
      <item name="android:textStyle">normal</item>
      <item name开发者_Python百科="android:typeface">normal</item>
    </style>
</resources>


This is quite simple : you are not overriding android default style, your just creating a new one which extends android:Widget.EditText. Thus, the style is not applied.

To correct this, into your theme definition, just add :

<item name="android:editTextStyle">@style/fap</item>

Now, each time Android instanciate an EditText, when it load default style values, it will find your fap style.

Edit: searching through android's source code is very usefull. Check https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/attrs.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/themes.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/EditText.java
for example.


EditText widget just can't get these parameters from an activity theme. It gets its default style from the android:editTextStyle parameter of the activity theme. So you have to create your own style:

<style name="MyEditText" parent="android:Widget.EditText">
  <item name="android:textSize">24sp</item>
  <item name="android:textColor">#FF0000</item>
  <item name="android:textStyle">normal</item>
  <item name="android:typeface">normal</item>
</style>

And then set it as EditText style in the activity theme:

<style name="fap" parent="android:Theme.Holo">
  <item name="android:editTextStyle">@style/MyEditText</item>
</style>

Hope this will work because I haven't tried this code.

0

精彩评论

暂无评论...
验证码 换一张
取 消