重写 editTextStyle 不适用于最新的材质组件基本样式

2024-02-23

在我的应用程序中,我使用Theme.MaterialComponents.Light.NoActionBar作为基本样式。这种风格,我称之为AppTheme,我正在尝试覆盖editTextStyle提供自定义样式com.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 with ?attr/textInputEditTextStyle, 有用。

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

Target 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>

所有上述荒谬的事情和我生命中的另一天都被扔进了垃圾桶,只是为了改变文字的颜色。啊啊啊啊啊安卓。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重写 editTextStyle 不适用于最新的材质组件基本样式 的相关文章

随机推荐

  • 我可以将现有 NuGet 包上传到 Azure DevOps 工件源吗?

    我目前正在从 TFS 2012 迁移到 Azure DevOps 2019 均为本地部署 使用旧服务器 我会从我们的一些构建中手动创建 NuGet 包 并托管这些包 nupkg文件共享上的文件 在 Visual Studio 中配置为包源
  • “无法初始化类 com.ibm.icu.impl.JavaTimeZone”是什么意思?

    发生错误 有关更多详细信息 请参阅错误日志 无法初始化类 com ibm icu impl JavaTimeZone 看看 http bugs debian org cgi bin bugreport cgi bug 600288 http
  • 如何使用 rxjs 5 观察对象变化

    我想监视一个对象 这样所有订阅者都会收到它的任何更改 我看到已经是了之前问过 https stackoverflow com questions 32683488 rxjs observing object updates and chan
  • 在 update() 转换期间绑定事件

    是否可以在转换期间将事件绑定到选择 例如 假设这是您的更新 g3 selectAll circles data dataFiltered function d return d token transition delay circleDe
  • 如何将一串键/值对转换为 HashTable 或 Dictionary 或者?

    在 VB NET 中 如何将以下字符串转换为某种键 值类型 例如哈希表 字典等 Name Fred Birthday 19 June 1906 ID 12345 我想提取生日或 ID 而不必将字符串拆分为数组 编辑 我不想将字符串拆分为数组
  • 在片段 android 的 CursorLoader 中显示进度对话框

    美好的一天 正如标题所说 任何人都知道如何在从片段内的 CursorLoader 加载数据时实现进度对话框 找不到这方面的任何例子 任何有关如何操作的链接或指南将受到高度赞赏 谢谢 我认为 Michal 的解决方案非常适合通过以下方式显示不
  • MySQL STR_TO_DATE() 函数返回 null

    我想将日期格式转换为MMMM dd yyyy to yyyy MM dd 我尝试使用以下内容 SET dt to STR TO DATE dateTo d m Y 但返回一个NULL value 我如何将我的日期转换为yyyy MM ddM
  • WPF - 将位图转换为 ImageSource

    我需要转换一个System Drawing Bitmap into System Windows Media ImageSource类 以便将其绑定到 WizardPage 扩展 WPF 工具包 的 HeaderImage 控件中 位图被设
  • 行列反转所形成的矩阵左上象限的最大和

    我正在研究一个 HackerRank 问题 该问题在反转行和列后找到 2N x 2N 矩阵的左上象限中元素的最大总和 例如 如果矩阵是 M 112 42 83 119 56 125 56 49 15 78 101 43 62 98 114
  • 强制 JSON.NET 将 xml 转换为 json 数组 [重复]

    这个问题在这里已经有答案了 我正在使用 JSON NET 将一些 XML 转换为 JSON 我的 XML 看起来像这样
  • 在 Woocommerce 中仅显示特定客户所在国家/地区的价格

    我已经使用 woocommerce 开发了一个目录 但是我需要能够对从英国境外访问该网站的用户隐藏产品价格由于我无法控制的原因 我找到了一些插件 可以让我根据访问者位置更改产品价格 但没有任何插件可以让我隐藏价格 是否有任何我错过的插件或任
  • GlobalFilter 与 WebFilter

    我正在 Spring Cloud Gateway 中迈出第一步 在学习过程中 我想更好地了解GlobalFilter 具体来说 我打算每次都应用一些过滤器every route 阅读文档 参考 https cloud spring io s
  • 当表单具有背景图像时,如何解决控件不必要的闪烁?

    问题概述 当我尝试隐藏一些标签 文本框和按钮时 我遇到了这个问题 所有这些控件在隐藏时都会闪烁 但其他不涉及的控件都很好 我不想要这种效果 另外 这不会发生在Show 仅发生在Hide 我尝试过的 删除背景图像解决了这个问题 但我确实希望我
  • 在 iOS 8 上使用 NSKeyedUnarchiver 取消存档 UIImage 对象返回 CGSizeZero 图像

    我的代码在 iOS 7 上运行 NSData imageData NSKeyedArchiver archivedDataWithRootObject self imageView image UIImage imageCopy NSKey
  • 如何在 PHP 8.0 中使用 ssh2?

    在安装 php 8 更新之前 我通读了所有不兼容性 只是发现为时已晚 我还没有找到对 ssh2 扩展的支持 我希望我只是错过了一些东西 我已经用谷歌搜索了几个小时 所有可能的线索都没有找到 谁能告诉我 ssh2 扩展的 php 8 版本吗
  • Spring在单元测试中嵌入了ldap服务器

    我目前正在尝试使用嵌入式 LDAP 服务器进行单元测试 在 Spring Security 中 您可以快速定义一个嵌入式 ldap 服务器 以便使用标签以及从指定 ldif 加载的一些示例数据进行测试 我将使用 Spring Ldap 执行
  • 致命错误,因为传递的是 Error 实例而不是预期的 Exception 实例

    我刚刚安装了 xampp 和 php 7 0 4 以及新的 cakephp 3 2 6 我做了很多研究 我发现的一切都表明它们是兼容的 然而 当我用浏览器打开应用程序时 蛋糕就会回复内部错误 关于要做什么的唯一指示是 xampp 在 apa
  • 获取当前背景

    我想获取当前的背景并以此为基础做一个条件 例如 我有一个带有下一个箭头的xml 如果背景 R drawable A 我想在按下下一个按钮时将背景更改为R drawable B 我定义了我的相对布局如下 final RelativeLayou
  • python中a=b=c是什么? [复制]

    这个问题在这里已经有答案了 我很困惑 连续相等 可以在 python 中使用 例如 a b c 这个语言特性叫什么 有什么我可以读到的吗 能化成4个等号吗 a b c d 这只是一种声明方式a and b等于c gt gt gt c 2 g
  • 重写 editTextStyle 不适用于最新的材质组件基本样式

    在我的应用程序中 我使用Theme MaterialComponents Light NoActionBar作为基本样式 这种风格 我称之为AppTheme 我正在尝试覆盖editTextStyle提供自定义样式com google and