更改颜色突出显示的 TextInputLayout 并提高提示的高度

Change Color Highlighted TextInputLayout and Raise the Height of the Hint

我在 Android Studio 中使用 TextInputLayout,当我点击它输入文本时,它突出显示为绿色,并且提示覆盖了我输入的一些文本。是否可以更改颜色并提出提示以查看键入的文本?谢谢!

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/lastNameTextInputLayout"
        android:layout_width="168dp"
        android:layout_height="47dp"
        app:layout_constraintBottom_toBottomOf="@+id/firstNameTextInputLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/firstNameTextInputLayout"
        app:layout_constraintTop_toTopOf="@+id/firstNameTextInputLayout">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/last_name" />
    </com.google.android.material.textfield.TextInputLayout>

由于静态高度,提示和文本相互重叠android:layout_height="47dp"

将高度更改为 wrap_content android:layout_height="wrap_content"

有一些属性可以改变背景颜色和提示颜色:

  • 提示文字颜色app:hintTextColor
  • 背景颜色app:boxBackgroundColor
  • 描边颜色app:boxStrokeColor

示例代码:

 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/lastNameTextInputLayout"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="@+id/firstNameTextInputLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/firstNameTextInputLayout"
            app:layout_constraintTop_toTopOf="@+id/firstNameTextInputLayout"
            app:boxBackgroundColor="@color/white"
            app:boxStrokeWidthFocused="1dp"
            app:boxStrokeColor="@color/black"
            app:hintTextColor="@color/black">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Last name"/>
        </com.google.android.material.textfield.TextInputLayout>

有关 TextInputLayout 属性的更多信息,请查看此 link