【问题标题】:How to change outline of Text Field in material.io?如何更改 material.io 中文本字段的轮廓?
【发布时间】:2026-01-03 16:35:02
【问题描述】:

我有两个文本字段 (Material.io),当我点击第一个时,轮廓颜色变为我在 app:boxStrokeColor="@color/button_colour" 中指定的颜色,但是当我选择另一个文本字段时,第一个文本字段的颜色变为灰色阴影。如何将此颜色更改为任何其他颜色?

【问题讨论】:

    标签: android textfield android-textinputlayout material-components material-components-android


    【解决方案1】:

    不要使用颜色,而是使用选择器

    app:boxStrokeColor="@color/myselector"
    

    选择器类似于:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:color="?attr/colorPrimary" android:state_focused="true"/>
      <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
      <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
      <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
    </selector>
    

    否则你可以:

    • 使用boxStrokeColor 的标准选择器并使用android:theme="@style/ThemeOverlay_til" 覆盖颜色
      <style name="ThemeOverlay_til">
        <item name="colorOnSurface">@color/....</item>
      </style>
    
    • 为您的TextInputLayout 使用自定义样式来覆盖颜色:

      <style name="Custom_OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="materialThemeOverlay">@style/MyMaterialThemeOverlay</item>
      </style>
    
      <style name="MyMaterialThemeOverlay" parent="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
        <item name="colorOnSurface">@color/....</item>
      </style>
    

    【讨论】: