Android: TextColor 在 EditText 的 android:textAppearance 中被忽略
Android: textColor ignored in android:textAppearance of the EditText
为什么 textColor
属性 在通过自定义 textAppearance
样式设置时被忽略?
<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>
XML中的设置样式:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
/>
出于某种原因,此样式未覆盖默认主题控件颜色。
我能够设置颜色的唯一方法是在 EditText
中设置 textColor
属性(但这不是我想要的):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:textColor="@color/blue"
/>
请注意,为 TextView
创建的带有 textColor
的自定义 textAppearance
可以正常工作。
我尝试用android.support.v7.widget.AppCompatEditText
或android.support.design.widget.TextInputEditText
替换EditText
,但结果是一样的。还是行不通。所以问题不在 EditText
实现中。
我发现了同样问题的问题 Why is textColor in android:textAppearance ignored?。很遗憾,没有答案。
仍然不知道为什么 textAppearance
在按照问题所述创建样式时忽略 textColor
,但我想我找到了另一种方法如何通过样式设置 textColor
,所以有人可能会觉得它有用。
我正在玩 EditText
颜色,所以我创建了自定义主题并尝试设置 editTextColor
属性,结果出人意料地有效:
<!-- 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>
设置主题:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:theme="@style/EditTextTheme"
/>
此时您有自定义 "@style/EditTextAppearance"
和 "@style/EditTextTheme"
。好处是,您可以 将 "@style/EditTextAppearance"
合并到 "@style/EditTextTheme"
中,如下所示:
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
...
<item name="editTextColor">@color/blue</item>
<item name="editTextStyle">@style/EditTextAppearance</item>
</style>
这样,您只需要在 EditText
:
中设置主题
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditTextTheme"
/>
目前的解决方案几乎是完美的,但如果 textColor
将设置为 textAppearance 样式会更好,所以我尝试从主题中删除 editTextColor
并将其替换为 textColor
属性 在 textAppearance 样式中设置 @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>
这样textAppearance
中包含的textColor
终于应用了!
在线的某处,textColor
的 wrong/default 值正在被拾取并应用。您可以通过在 XML 中为 EditText
设置 android:textColor="@null"
来强制使用您在 android:textAppearance
中定义的 android:textColor
。
android:textColor
在 EditText
的 android:textAppearance
中被忽略,因为 android:editTextColor
属性总是覆盖 EditText
的文本颜色(不要问我为什么,它关于疯狂的机器人)。
但是,如果您将此属性添加到您的主题,一切正常:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:editTextColor">@null</item>
</style>
为什么 textColor
属性 在通过自定义 textAppearance
样式设置时被忽略?
<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>
XML中的设置样式:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
/>
出于某种原因,此样式未覆盖默认主题控件颜色。
我能够设置颜色的唯一方法是在 EditText
中设置 textColor
属性(但这不是我想要的):
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:textColor="@color/blue"
/>
请注意,为 TextView
创建的带有 textColor
的自定义 textAppearance
可以正常工作。
我尝试用android.support.v7.widget.AppCompatEditText
或android.support.design.widget.TextInputEditText
替换EditText
,但结果是一样的。还是行不通。所以问题不在 EditText
实现中。
我发现了同样问题的问题 Why is textColor in android:textAppearance ignored?。很遗憾,没有答案。
仍然不知道为什么 textAppearance
在按照问题所述创建样式时忽略 textColor
,但我想我找到了另一种方法如何通过样式设置 textColor
,所以有人可能会觉得它有用。
我正在玩 EditText
颜色,所以我创建了自定义主题并尝试设置 editTextColor
属性,结果出人意料地有效:
<!-- 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>
设置主题:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/EditTextAppearance"
android:theme="@style/EditTextTheme"
/>
此时您有自定义 "@style/EditTextAppearance"
和 "@style/EditTextTheme"
。好处是,您可以 将 "@style/EditTextAppearance"
合并到 "@style/EditTextTheme"
中,如下所示:
<style name="EditTextTheme" parent="Base.V7.Widget.AppCompat.EditText">
...
<item name="editTextColor">@color/blue</item>
<item name="editTextStyle">@style/EditTextAppearance</item>
</style>
这样,您只需要在 EditText
:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditTextTheme"
/>
目前的解决方案几乎是完美的,但如果 textColor
将设置为 textAppearance 样式会更好,所以我尝试从主题中删除 editTextColor
并将其替换为 textColor
属性 在 textAppearance 样式中设置 @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>
这样textAppearance
中包含的textColor
终于应用了!
在线的某处,textColor
的 wrong/default 值正在被拾取并应用。您可以通过在 XML 中为 EditText
设置 android:textColor="@null"
来强制使用您在 android:textAppearance
中定义的 android:textColor
。
android:textColor
在 EditText
的 android:textAppearance
中被忽略,因为 android:editTextColor
属性总是覆盖 EditText
的文本颜色(不要问我为什么,它关于疯狂的机器人)。
但是,如果您将此属性添加到您的主题,一切正常:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:editTextColor">@null</item>
</style>