微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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">

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。