TextInputLayout - boxBackgroundColor 样式属性在 Android 中不起作用

问题描述

我在我的应用程序中使用了轮廓文本输入布局。我想更改焦点的背景颜色,但我无法使用 BoxBackgroundColor 样式属性实现它。我的布局和样式代码添加如下:

布局

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_username"
    style="@style/TextInputLoginTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginRight="16dp"
    app:hintTextColor="@color/login_hint_color">

    <com.google.android.material.textfield.TextInputEditText
        style="@style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.Dense"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username"
        android:imeOptions="actionNext"
        android:textColor="#38465A"
        android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>

主题

<style name="TextInputLoginTheme" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="BoxstrokeColor">@color/login_outlined_stroke_color</item>
    <item name="BoxBackgroundColor">@color/login_Box_background_color</item>
    <item name="BoxBackgroundMode">outline</item>
</style>

login_Box_background_color

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:color="@color/white" android:state_focused="true" />
   <item android:color="#EFF2F5" android:state_hovered="true" />
   <item android:color="#EFF2F5" android:state_enabled="false" />
   <item android:color="#EFF2F5" />
</selector>

物质依赖

implementation 'com.google.android.material:material:1.2.1'

焦点上的背景颜色没有变成白色。

解决方法

仅填充框支持背景颜色。当与 BOX_BACKGROUND_FILLED 以外的框变体一起使用时,框背景颜色可能无法按预期工作。

Source

app:boxBackgroundMode="filled" // 添加

我的替代解决方案;

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til_username"
            style="@style/TextInputLoginTheme"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginRight="16dp"
            app:boxBackgroundMode="filled"
            app:hintTextColor="@color/login_hint_color">