【问题标题】:TextInputLayout - boxBackgroundColor style property not working in AndroidTextInputLayout - boxBackgroundColor 样式属性在 Android 中不起作用
【发布时间】:2020-12-24 18:00:12
【问题描述】:

我在我的应用程序中使用了大纲文本输入布局。我想改变焦点的背景颜色,但我无法使用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'

焦点上的背景颜色不会变为白色。

【问题讨论】:

  • 嗯,您是否尝试过在 android:theme 属性中使用您的样式资源而不是 style ?通常颜色更改需要使用android:theme 属性。
  • @FurkanYurdakul:我的要求是更新一种材料设计风格,如何将其移至主题?能否提供样品?

标签: android android-textinputlayout android-textinputedittext


【解决方案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">

【讨论】:

  • 我想保留轮廓框。因此,使用此方法无法实现我的要求。我将此标记为已接受的答案。
猜你喜欢
  • 2019-12-05
  • 2020-12-17
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2021-03-07
  • 2018-02-06
  • 2023-02-06
  • 1970-01-01
相关资源
最近更新 更多