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

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


当前回答

为该编辑文本使用android:background属性。将可绘制的文件夹图像传递给它。 例如,

android:background="@drawable/abc.png"

其他回答

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

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

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

android:background="@drawable/bg_edit_text"

为该编辑文本使用android:background属性。将可绘制的文件夹图像传递给它。 例如,

android:background="@drawable/abc.png"

在XML布局中使用:

android:backgroundTint="@color/colorPrimary"

或者在Java代码中复制这个方法:

public void changeLineColorInEditText(EditText editText, int color) {
        editText.setBackgroundTintList(ColorStateList.valueOf(color));
    }

像这样使用它:

changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));

最好的方法是使用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级别的应用程序。

这是最好的工具,你可以使用所有的观点,它的免费许多感谢@Jérôme Van Der Linden。

Android Holo颜色生成器允许您轻松地创建Android组件,如EditText或旋转与您自己的Android应用程序的颜色。它将生成所有必要的9个补丁资产加上相关的XML绘图和样式,您可以直接复制到您的项目。

http://android-holo-colors.com/

更新1

这个域名似乎过期了,但是这个项目是开源的,你可以在这里找到

https://github.com/jeromevdl/android-holo-colors

试一试

这张图片放在EditText的背景

android:background="@drawable/textfield_activated"


更新2

对于API 21或更高版本,你可以使用android:backgroundTint

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Underline color change"
        android:backgroundTint="@android:color/holo_red_light" />

更新3 现在我们有了AppCompatEditText的支持

注意:我们需要使用app:backgroundTint而不是android:backgroundTint

<android.support.v7.widget.AppCompatEditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="Underline color change"
    app:backgroundTint="@color/blue_gray_light" />

更新4 AndroidX版本

  <androidx.appcompat.widget.AppCompatEditText

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