我在我的布局xml文件中创建一个EditText

但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?


当前回答

最好的方法是使用AppCompatEditText和应用命名空间的backgroundTint属性。即。

    <android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"      
    app:backgroundTint="YOUR COLOR"
    android:layout_height="wrap_content" />

当我们使用android:backgroundTint时,它只会在API21或以上工作,但app:backgroundTint适用于所有API级别的应用程序。

其他回答

你可以通过着色背景来改变颜色 < EditText android: backgroundTint = " @color /红" / >

线条的颜色由EditText的background属性定义。要改变它,你应该改变布局文件中的android:background。

我应该注意的是,这种风格是通过使用9个补丁绘制来实现的。如果你查看SDK,你可以看到EditText的背景是这样的图片:

要改变它,你可以在图像处理程序中打开它,并将其涂成所需的颜色。保存为bg_edit_text.9.png,然后把它放在你的可绘制文件夹。现在你可以像这样应用它作为你的EditText的背景:

android:background="@drawable/bg_edit_text"

它非常简单(要求:最低API 21)…

转到xml并选择EditText字段 在右侧,你可以看到“Attributes”窗口。选择“查看所有属性” 只要搜索“tint” 添加/更改backgroundTint为所需的十六进制颜色(例如#FF0000)

保持编码……:)

我不喜欢之前的答案。最好的解决方案是使用:

<android.support.v7.widget.AppCompatEditText

      app:backgroundTint="@color/blue_gray_light" />

android:backgroundTint for EditText仅适用于API21+。因此,我们必须使用支持库和AppCompatEditText。

注意:我们必须使用app:backgroundTint而不是android:backgroundTint

AndroidX版本

<androidx.appcompat.widget.AppCompatEditText

      app:backgroundTint="@color/blue_gray_light" />

可拉的/ bg_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="@color/colorDivider" />
        </shape>
    </item>
</layer-list>

设置为EditText

<android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_edittext"/>