【问题标题】:Android Material Components theme - change color of TextInputLayout/TextInputEditText label and underline when field is focusedAndroid Material Components 主题 - 当字段聚焦时更改 TextInputLayout/TextInputEditText 标签和下划线的颜色
【发布时间】:2019-07-15 18:55:15
【问题描述】:

我正在尝试切换到新的 Material Components 主题,但我似乎无法更改的一件事是标签的 TextInputLayout/TextInputEditText 颜色和字段聚焦时的下划线。

当我使用 AppCompat 主题时,它为此使用了 colorAccent,但新的 MaterialComponents 主题使用了 colorPrimary。

如何覆盖 MaterialComponents 主题以仅在这种情况下使用 colorAccent?

我尝试过的事情:

  • 为 TextInputLayout 扩展 Widget.MaterialComponents 样式 和 TextInputEditText 并覆盖 colorPrimary
  • 为 TextInputLayout 扩展 Widget.MaterialComponents 样式 和 TextInputEditText 并覆盖 colorAccent
  • 在 TextInputEditText 上为 backgroundTint 使用选择器颜色

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    您可以先定义新主题 - 例如:

    <style name="Theme2" parent="AppTheme">
        <item name="colorPrimary">@android:color/holo_green_dark</item>
    </style>
    

    ...然后像这样使用它:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/filter_box_hint"
        android:theme="@style/Theme2">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFilter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textNoSuggestions"
            android:maxLines="1"
            android:paddingTop="4dp"
            android:theme="@style/Theme2" />
    </com.google.android.material.textfield.TextInputLayout>
    

    结果(请忽略框,它是差异故事的一部分):

    【讨论】:

    • 我需要 TextInputLayout 和 TextInputEditText 的主题,并且我需要覆盖 colorPrimary 而不是 colorAccent,但一般概念有效。谢谢!
    • 对我来说是 colorAccent)
    【解决方案2】:

    无需定义新主题即可使用:

    app:boxStrokeColor="@color/colorAccent"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2020-01-20
      • 1970-01-01
      相关资源
      最近更新 更多