问题描述
在我的一个应用程序中,我使用Theme.MaterialComponents.Light.NoActionBar
作为基本样式.在我称为AppTheme
的这种样式中,我试图覆盖editTextStyle
为com.google.android.material.textfield.TextInputEditText
提供自定义样式(根据源代码,它使用R.attr.editTextStyle
作为默认样式).
In an app of mine, I'm using the Theme.MaterialComponents.Light.NoActionBar
as a base style. In this style, which I call AppTheme
, I'm trying to override editTextStyle
to provide a custom style for com.google.android.material.textfield.TextInputEditText
(according to the source code, it uses R.attr.editTextStyle
as a default style).
这是我当前的主题,与TIEditText和TILayout相关:
This is my current theme, related to the TIEditText and TILayout:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
[ primary and secondary colors, OnColors, etc.]
<item name="editTextStyle">@style/AppTheme.TextInputEditText</item>
<item name="textInputStyle">@style/AppTheme.TextInputLayout</item>
[ Custom attribute for testing, defined in attrs.xml ]
<item name="textInputEditTextStyle">@style/AppTheme.TextInputEditText</item>
</style>
由于某些原因,即使我设置了editTextStyle
,如果我在代码中使用它,它也不会被应用:
For some reason, even though I set editTextStyle
, if I use it in code, it does not get applied:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilFirstName"
style="?attr/textInputStyle"
android:hint="@string/label_firstname"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/firstName"
style="?attr/editTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@={viewModel.firstName}" />
</com.google.android.material.textfield.TextInputLayout>
但是,如果我将firstName
的样式替换为?attr/textInputEditTextStyle
,它会起作用.
However if I replace the style of firstName
with ?attr/textInputEditTextStyle
, it works.
为什么不能在默认主题中覆盖editTextStyle
?到底是怎么回事?
Why can't I override editTextStyle
in the default theme? What the hell is going on?
目标SDK为28,minSDK为21,材料库版本为1.1.0-alpha06
Target SDK is 28, minSDK is 21, Material library version is 1.1.0-alpha06
推荐答案
之所以会发生这种情况,是因为TextInputLayout
的默认样式会使用materialThemeOverlay
属性覆盖 editTextStyle
.
It happens because the default styles of the TextInputLayout
override the editTextStyle
using the materialThemeOverlay
attribute.
例如,Widget.MaterialComponents.TextInputLayout.FilledBox
具有以下默认样式:
For example the Widget.MaterialComponents.TextInputLayout.FilledBox
has this default style:
<style name="Widget.MaterialComponents.TextInputLayout.FilledBox" parent="Base.Widget.MaterialComponents.TextInputLayout">
<item name="materialThemeOverlay">
@style/ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox
</item>
....
</style>
<style name="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox">
<item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.FilledBox</item>
</style>
这篇关于覆盖editTextStyle不适用于最新的Material Components基本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!