android - 覆盖 editTextStyle 不适用于最新的 Material Components 基本样式

标签 android android-edittext android-textinputlayout material-components material-components-android

在我的一个应用程序中,我使用的是 Theme.MaterialComponents.Light.NoActionBar作为基本风格。在这种风格中,我称之为 AppTheme ,我正在尝试覆盖 editTextStylecom.google.android.material.textfield.TextInputEditText 提供自定义样式(根据源代码,它使用 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>

出于某种原因,即使我设置了 editTextStyle ,如果我在代码中使用它,它不会被应用:
<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 , 有用。

为什么我不能覆盖 editTextStyle在默认主题中?这到底是怎么回事?

目标SDK为28,minSDK为21, Material 库版本为1.1.0-alpha06

最佳答案

让我们跳过我们都认识到Android主题和风格是人类设计的最荒谬的黑客和猜测荒地的部分。

这是对上一个答案的扩展。同样愚蠢的“黑客”。我能够设置 TextInputEditText 的样式通过设置 editTextStyle ,但不是它直观所属的位置,而是在自定义 materialThemeOverlay 中嵌套在为 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>

以上所有的荒谬和我生命中的另一天都被扔进了垃圾桶,只是为了改变文字的颜色。 Thaaaaanks Aaaaaandroid。

关于android - 覆盖 editTextStyle 不适用于最新的 Material Components 基本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044075/

相关文章:

android - 覆盖 edtitext 的 getText() 方法会导致 stackoverflow,为什么?

android - 如何显示加载对话框

android - Android EditText崩溃应用

Android:TextInputLayout - 自定义提示、底线和错误消息的颜色

Android 使用形状、选择器或列表针对不同状态的不同 EditText 背景

android - EditText.setError 不显示错误文本,只显示图标

android - 为什么 `TextInputLayout` 会使用样式颜色来绘制光标而不是下划线?

android - 在 Android 中隐藏主页和后退按钮

java - 使用 OpenCV 求矩形面积

android - 发送电子邮件时拦截消息 "no application can perform this action"- Android