材质TexInputLayout更改颜色

时间:2020-06-03 13:08:38

标签: android android-studio android-layout

我需要更改颜色,但我不知道该怎么做。你能帮我吗?

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/contraseña"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="440dp"
    android:layout_marginEnd="16dp"

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Contraseña"
        android:inputType="textPersonName" />
</com.google.android.material.textfield.TextInputLayout>

enter image description here

我想将灰色更改为白色。

1 个答案:

答案 0 :(得分:0)

要更改边框颜色和hintTextColor,可以使用

app:boxStrokeColor="your color"
app:hintTextColor="your color" /*This changes the color of hint when it's collapsed 
and moved to top that's when user enters a character.*/
app:textColorHint="your color" //This is the text color of uncollapsed hint that's the default state.

此外,如果您要创建样式,则这是我使用的完整样式,可以根据需要进行修改。

<style name="TextInputLayoutAppearanceOutLined" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="hintTextAppearance">@style/HintText</item>
    <item name="helperTextTextAppearance">@style/HelperText</item>
    <item name="counterTextAppearance">@style/HelperText</item>

    <item name="android:textColor">@color/dark_grey</item>
    <item name="android:textColorHint">@color/dark_grey</item>
    <item name="hintTextColor">@color/colorAccent</item>
    <item name="boxStrokeColor">@color/colorAccent</item>
    <item name="startIconTint">@color/colorAccent</item>
    <item name="endIconTint">@color/colorAccent</item>
    <item name="boxBackgroundColor">@color/white</item>

    <item name="boxCornerRadiusBottomEnd">10dp</item>
    <item name="boxCornerRadiusBottomStart">10dp</item>
    <item name="boxCornerRadiusTopEnd">10dp</item>
    <item name="boxCornerRadiusTopStart">10dp</item>

    <item name="boxStrokeWidthFocused">2dp</item>


    <item name="hintEnabled">true</item>
    <!--<item name="hintAnimationEnabled">true</item>-->

</style>

这些是更改HelperText或HintText外观的样式元素:

<style name="HintText" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">12sp</item>
</style>

<style name="HelperText" parent="TextAppearance.Design.HelperText">
    <item name="android:textSize">12sp</item>
</style>

并将其设置为TextInputlayoutstyle="@style/TextInputLayoutAppearanceOutLined"。无论样式中提到什么,您都可以在XML布局标记中使用它。

此外,与Android的开发人员文档不同,材料TextInputLayout的官方文档非常重要且易于理解,您应该阅读它们一次才能进一步了解here