首页 > 解决方案 > 更改 TextInputLayout 标签 TextView 的背景颜色

问题描述

所以我试图改变TextInputLayout/EditText上标签背景的颜色。大多数其他问题似乎是指文本的颜色,但我观察到的标签的默认功能是“OutlinedBox”样式上标签的背景是否与层次结构中其上方布局的背景颜色匹配. https://imgur.com/mc5JB9b https://imgur.com/pjST45F (抱歉没有足够的声誉来发布图片)

如您所见,在前。1、TextInputLayout上面的布局是灰色的,所以Label TextView背景也变成了灰色。在前。2、TextInputLayout上面的布局是白色的,所以Label Text的背景也变成了白色。因为这在没有我输入的情况下发生了变化(它没有使用像“colorAccent”这样的颜色,它适用于整个板,而是使用基于布局背景颜色的不同颜色)。我想找出的是,是否有一个我可以维护/编辑的颜色属性,最好是通过样式,这将允许我选择该颜色,而不是默认为层次结构中其上方的任何布局?

我尝试在 Style 中设置各种颜色以尝试覆盖默认行为,但没有找到这样做的。

这是数量的 TextInputLayout/EditText。

     <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilAmount"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="0dp"
                android:layout_height="55dp"
                android:layout_marginRight="@dimen/margin_between_boxes"
                android:layout_weight="1"
                android:hint="@string/et_amount"
                app:boxBackgroundColor="@color/white"
                app:boxStrokeColor="@color/editText_background"
                app:boxStrokeWidth="3dp"
                app:hintTextAppearance="@style/TextLabel">


                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etAmount"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fontFamily="@font/neue_haas_grotesk_display_regular"
                    android:inputType="number"
                    android:maxLength="7"
                    android:paddingLeft="13dp"
                    android:textAlignment="viewStart"
                    android:textColorHint="@android:color/black"
                    android:textSize="14sp"
                    android:layout_gravity="start" />
            </com.google.android.material.textfield.TextInputLayout>

这是用于hintTextAppearance的样式

      <style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/label_text</item>
    <item name="android:textColor">@color/label_text</item>
    <item name="android:textSize">12sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@android:color/black</item>
    <item name="colorControlNormal">@color/label_text</item>
    <item name="colorControlActivated">@color/label_text</item>
    <item name="android:colorControlHighlight">@color/label_text</item>
    <item name="android:textColorPrimary">@color/label_text</item>
    <item name="android:textColorSecondary">@color/label_text</item>
</style>

我希望能够将标签 TextView 的背景颜色编辑为我选择的任何颜色。

标签: javaandroidlabelandroid-textinputlayoutandroid-textinputedittext

解决方案


推荐阅读