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



我在应用程序中使用了Outlined文本输入布局。我想更改焦点上的背景颜色,但无法使用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以外的长方体变体一起使用时,长方体背景颜色可能无法按预期工作。

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

最新更新