Android 重写editTextStyle不会';t使用最新的材质组件基本样式
在我的一个应用程序中,我使用Android 重写editTextStyle不会';t使用最新的材质组件基本样式,android,android-edittext,android-textinputlayout,material-components,material-components-android,Android,Android Edittext,Android Textinputlayout,Material Components,Material Components Android,在我的一个应用程序中,我使用主题.MaterialComponents.Light.NoActionBar作为基本样式。在我称之为AppTheme的这个样式中,我试图覆盖editTextStyle,为com.google.android.material.textfield.textinputtext提供一个自定义样式(根据源代码,它使用R.attr.editTextStyle) 这是我当前的主题,与TIEditText和TILayout相关: <style name="AppTheme"
主题.MaterialComponents.Light.NoActionBar
作为基本样式。在我称之为AppTheme
的这个样式中,我试图覆盖editTextStyle
,为com.google.android.material.textfield.textinputtext
提供一个自定义样式(根据源代码,它使用R.attr.editTextStyle
)
这是我当前的主题,与TIEditText和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>
但是,如果我将firstName
的样式替换为?attr/textinputtextstyle
,它就可以工作了
为什么我不能在默认主题中覆盖editTextStyle
?到底怎么回事
目标SDK为28,minSDK为21,材料库版本为1.1.0-alpha06
出于某种原因,即使我设置了editTextStyle,如果我在代码中使用它,它也不会被应用
之所以会发生这种情况,是因为文本输入布局的默认样式使用materialThemeOverlay
属性覆盖了编辑文本样式
例如,Widget.MaterialComponents.TextInputLayout.Filled框具有以下默认样式:
<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>
@style/ThemeOverlay.MaterialComponents.TextInputItemText.Filled框
....
@style/Widget.MaterialComponents.textInputItemText.Filled框
让我们回顾一下我们都认识到Android主题和风格是人类有史以来最荒谬的黑客和猜测荒原的部分
这是对前面答案的扩展。同样愚蠢的“黑客”。我可以通过设置editTextStyle
来设置textInputItemText
的样式,但不是在它直观上属于的位置,而是在一个自定义材质中,在为textInputStyle
定义的样式中嵌套该材质。证人:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- works fine -->
<item name="textInputStyle">@style/AppTheme.TextInputLayoutStyle</item>
<!-- should work fine, doesn't work, happily ignored -->
<!-- <item name="editTextStyle">@style/AppTheme.TextInputEditTextStyle</item> -->
</style>
<style name="AppTheme.TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<!-- other props (boxBackgroundMode, boxBackgroundColor, boxStrokeColor, etc) -->
<!-- can we set editTextStyle from here? Of course not! We should magically know we need a material theme overlay-->
<item name="materialThemeOverlay">@style/AppTheme.MaterialThemeOverlay</item>
</style>
<!-- style inception! a style, child of another style, whose only purpose is to refer to yet another style -->
<style name="AppTheme.MaterialThemeOverlay">
<item name="editTextStyle">@style/AppTheme.TextInputEditTextStyle</item>
</style>
<!-- finally, the style we SHOULD have been able to set from the theme -->
<style name="AppTheme.TextInputEditTextStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:textColor">@color/white</item>
</style>
@style/AppTheme.TextInputLayoutStyle
@样式/AppTheme.MaterialThemeOverlay
@style/AppTheme.textinputtextstyle
@颜色/白色
所有上述的荒谬和另一天我的生活扔在垃圾桶,只是为了改变文字的颜色。Thaaanks aaaaandroid.我也有同样的问题。浪费了很多时间,仍然不明白为什么不应用此属性。请解释为什么选择“覆盖MaterialComponents样式”,而不是添加自定义默认定义?:)我正在尝试应用它,但我不明白它为什么有效。谢谢:)@JosselinePerdomo这是材质组件为TextInputLayout.FilledBox
提供的默认样式。我解释了为什么style=“?attr/editTextStyle”
不起作用。它是在应用程序主题级别定义的默认属性。它由组件本身覆盖(使用相同的attr
覆盖materialThemeOverlay
)。相反,textinputtextstyle
之所以有效,是因为它是一个未被覆盖的自定义属性。非常感谢)在我使用您的解决方案之前,这太令人困惑了。对我来说,所有的风格都只是风格,而这一个特别的是最夸张的风格这太荒谬了,谷歌。他们真的需要重新设计整个主题系统。谢谢你为我省下了无数的时间来解决这个问题。
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- works fine -->
<item name="textInputStyle">@style/AppTheme.TextInputLayoutStyle</item>
<!-- should work fine, doesn't work, happily ignored -->
<!-- <item name="editTextStyle">@style/AppTheme.TextInputEditTextStyle</item> -->
</style>
<style name="AppTheme.TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<!-- other props (boxBackgroundMode, boxBackgroundColor, boxStrokeColor, etc) -->
<!-- can we set editTextStyle from here? Of course not! We should magically know we need a material theme overlay-->
<item name="materialThemeOverlay">@style/AppTheme.MaterialThemeOverlay</item>
</style>
<!-- style inception! a style, child of another style, whose only purpose is to refer to yet another style -->
<style name="AppTheme.MaterialThemeOverlay">
<item name="editTextStyle">@style/AppTheme.TextInputEditTextStyle</item>
</style>
<!-- finally, the style we SHOULD have been able to set from the theme -->
<style name="AppTheme.TextInputEditTextStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:textColor">@color/white</item>
</style>