温馨提示:本文翻译自stackoverflow.com,查看原文请点击:textview - Issue: change border color or box stroke for unfocused TextInputLayout in android
android android-textinputlayout material-components-android textview android-textattributes

textview - 问题:在Android中更改未聚焦的TextInputLayout的边框颜色或框描边

发布于 2020-04-22 14:25:55

我有一个非常具体的问题,即当文本输入框不集中时,如何更改文本框的轮廓。我似乎找不到属性来更改“未聚焦”文本框边框的颜色。

这是我要执行的操作的直观示例:

此(文本框):边框的颜色不是白色。目前还没有集中。单击它后,它变成白色:

我不知道需要更改什么,似乎没有属性可以更改它。

我也使用了材质设计文本输入布局样式,尽管我看不到是否会影响它。

这是我的文本框的xml代码:

 <other layouts ... >
     <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_gravity="bottom"
            android:layout_margin="5dp"
            android:background="@drawable/item_recycler_view">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/dialog_text_input_layout"
                style="@style/Widget.AppTheme.TextInputLayoutList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Quick Add..."
                android:textColorHint="@color/colorWhite"
                app:boxStrokeColor="@color/colorWhite"
                app:errorEnabled="true"
                >

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/dialog_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:maxLines="1"
                    android:textColor="@color/colorWhite"
                    android:textSize="14sp" />
            </android.support.design.widget.TextInputLayout>
        </RelativeLayout>
 </other layouts...>

这是我为此使用的样式:

<style name="TextAppearance.AppTheme.TextInputLayout.HintTextAlt" parent="TextAppearance.MaterialComponents.Subtitle2">
    <item name="android:textColor">@color/colorWhite</item>
</style>

<style name="Widget.AppTheme.TextInputLayoutList" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="hintTextAppearance">@style/TextAppearance.AppTheme.TextInputLayout.HintTextAlt</item>
    <item name="boxStrokeColor">@color/colorWhite</item>
    <item name="boxCornerRadiusBottomEnd">5dp</item>
    <item name="boxCornerRadiusBottomStart">5dp</item>
    <item name="boxCornerRadiusTopEnd">5dp</item>
    <item name="boxCornerRadiusTopStart">5dp</item>
    <item name="android:layout_margin">5dp</item>
</style>

谢谢,欢迎任何帮助或建议!

查看更多

提问者
Varun Govind
被浏览
261
Amjad 2018-09-06 09:28

如果要在未聚焦模式下为轮廓框设置颜色,而不是默认黑色,则必须在colors.xml文件中添加此行,以覆盖轮廓框的默认颜色。

照原样复制此行。您可以将颜色更改为所需的颜色。

<color name="mtrl_textinput_default_box_stroke_color">#fff</color>

直到现在为止,它都可以使用,要对TextInputLayout进行更多控制,可以在styles.xml中添加此样式

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">#fff</item>
    <item name="boxStrokeWidth">2dp</item>
</style>

然后将主题添加到TextInputLayout

android:theme="@style/TextInputLayoutStyle"