如何更改EditText中的行颜色

267

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

但是,我想要将EditText中的线条颜色从Holo更改为(例如)红色。 如何实现呢?

输入图片描述

22个回答

385

这是你可以用于所有视图的最佳工具,并且完全免费,非常感谢@Jérôme Van Der Linden

Android Holo Colors生成器允许您轻松创建Android组件,例如EditText或下拉菜单,并使用自己的颜色为Android应用程序定制。它将生成所有必要的九片拉伸位图和相关的XML可绘制和样式,您可以直接复制到项目中。

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

更新1

此域名似乎已过期,但该项目是开放源代码,您可以在此处找到

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

试试吧

将此图像放在EditText的背景中

android:background="@drawable/textfield_activated"

enter image description here


更新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" />

链接似乎失效了?这个软件还能下载到吗? - Coding John
2
@CodingJohn 这个项目是开源的,你可以在这里找到 https://github.com/jeromevdl/android-holo-colors - MilapTank
谁说我们必须使用“app:backgroundTint”而不是“android:backgroundTint”? - user924
1
如果您需要“向后兼容性”,那么可以使用 app:*** OW android: ***。 - MilapTank
android:backgroundTint工作得很好。谢谢! - claudiodfc
应用程序:backgroundTint 会覆盖小部件中的 android:background,如果您想同时更改两者,则必须在 FrameLayout 包装器中设置背景。 - redocoder

233

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

<android.support.v7.widget.AppCompatEditText

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

EditTextandroid:backgroundTint 只在 API21+ 上起作用,因此我们必须使用支持库以及 AppCompatEditText

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

AndroidX 版本

<androidx.appcompat.widget.AppCompatEditText

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


4
这是最好的解决方案!简单易用,适用于所有API级别。谢谢! - Ivo Stoyanov
9
如果你使用了appcompat库,就不需要这个了。EditText会自动转换为AppCompatEditText,并应用背景色调。 - stephane k.
非常感谢。它适用于API 16+。最佳解决方案。 - André Luiz Reis
2
能否通过编程设置app:backgroundTint?我找到了一个方法 'setBackgroundTint'。 - Drunken Daddy
1
背景色调更改整个背景色调,而不是下划线。 - Code Wiget
显示剩余2条评论

80

你也可以通过对EditText的背景进行着色来快速更改EditText的下划线颜色,方法如下:

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Something or Other"
            android:backgroundTint="@android:color/holo_green_light" />

19
仅适用于API 21或更高版本。有没有办法让它在较低的API级别上工作? - Vucko
请查看以下反转解决方案:https://dev59.com/Il8d5IYBdhLWcg3wpzr4 - powder366
@mladj0ni 很酷,点赞了。 - Vucko
3
对于较低的API级别,请仅使用“backgroundTint”而不是“android:backgroundTint”。来源:https://dev59.com/Il8d5IYBdhLWcg3wpzr4#29400711 - Hasaan Ali
这会改变整个背景颜色,而不是线条颜色。 - Code Wiget

48

对于21以下的API,您可以在EditText中使用主题属性,将下面的代码放入样式文件中。

<style name="MyEditTextTheme">
    <item name="colorControlNormal">#FFFFFF</item>
    <item name="colorControlActivated">#FFFFFF</item>
    <item name="colorControlHighlight">#FFFFFF</item>
</style>

EditText 中使用此样式:

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="@dimen/user_input_field_height"
    android:layout_marginTop="40dp"
    android:hint="@string/password_hint"
    android:theme="@style/MyEditTextTheme"
    android:singleLine="true" />

你知道如何在样式文件中添加透明度/不透明度吗?就像它应该带有正常颜色和一个透明度,一旦他们开始输入,它应该采用激活或突出显示的颜色。 - ASN
1
为此,您需要添加一个文本观察器监听器,并在“onTextChanged”方法中动态设置不透明度。 - Rahul
哦。所以它不能被添加到样式或选择器文件中,而应该动态完成? - ASN
@ASN 不透明度以十六进制颜色的前两个字符00-FF表示,例如:#A1FAFAFA。 - aimiliano

26

通过编程,您可以尝试:

editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);

2
Xamarin.Forms效果的最佳答案 - mister_giga

16

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

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

输入图像描述

输入图像描述

继续编码.......


2
最低API 21。 - Maihan Nijat
3
改变背景色调会改变背景。这个答案是不正确的。 - Code Wiget

15

你可以通过以下代码来编程地改变EditText的颜色:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));


这就是我要找的!谢谢。 - Aphicha Intaragumhaeng
1
如果 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { editText.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE)); } - Bay

11

我认为最好的方式是通过主题:

<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/action_blue</item>
        <item name="colorControlHighlight">@color/action_blue</item>
</style>

<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">wrap_content</item>
     <item name="android:textSize">13sp</item>
     <item name="android:theme">@style/MyEditTextTheme</item>
</style>

<android.support.v7.widget.AppCompatEditText
            style="@style/AddressBookStyle"/>

2
这个在使用style标签时对我不起作用,但是在使用android:theme标签时可以。 - aimiliano

9

要改变Edittext的下划线颜色:

如果你想让整个应用程序共享此样式,则可以按照以下方式操作。

(1) 进入styles.xml文件。您的AppTheme继承了Theme.AppCompat.Light.DarkActionBar的父类(在我的情况下),将成为应用程序中所有样式文件的基本父类。将其名称更改为“AppBaseTheme”。在其下方创建另一个样式,其名称为AppTheme,并继承您刚刚编辑的AppBaseTheme。它看起来像下面这样:

<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
    <item name="colorPrimary">@color/material_brown_500</item>
    <item name="colorPrimaryDark">@color/material_brown_700</item>
    <item name="colorAccent">@color/flamingo</item>

<style name="AppTheme" parent="AppBaseTheme">
    <!-- Customize your theme here. -->
</style>

将“colorAccent”更改为您希望EditText行颜色的任何颜色。

(2) 如果您有其他values文件夹与style.xml,请注意以下步骤非常重要。因为该文件将从您之前的父xml文件继承。例如,我有一个values-19/styles.xml。这是专门为Kitkat及以上版本而设计的。将其父级更改为AppBaseTheme,并确保摆脱“colorAccent”,以使其不覆盖父级的颜色。还需要保留特定于版本19的项。然后它将看起来像这样。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

7
该行的颜色由EditText的背景属性定义。要更改它,您应该在布局文件中更改android:background
我应该指出,这种样式是通过使用9-patch可绘制图形实现的。如果您查看SDK,您会发现EditText的背景是这张图片: textfield_activated_holo_light.9.png 要更改它,您可以在图像处理程序中打开它并将其涂上所需的颜色。将其保存为bg_edit_text.9.png,然后将其放入drawable文件夹中。现在,您可以将其应用为EditText的背景,如下所示:
android:background="@drawable/bg_edit_text"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接