如何在EditText中更改线条颜色

Ale*_*lex 177 xml layout android styles

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

但我想将EditText中的颜色线从Holo更改为(例如)红色.怎么做?

在此输入图像描述

Mil*_*ank 268

这是您可以用于所有视图的最佳工具,非常感谢@ JérômeVanDer Linden.

Android Holo颜色生成器允许您轻松地EditText为Android应用程序创建具有自己颜色的Android组件,例如旋转器或微调器.它将生成所有必需的九个补丁资源以及相关的XML drawable和样式,您可以将它们直接复制到项目中.

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

更新1

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

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

试试吧

这个图像放在背景中 EditText

android:background="@drawable/textfield_activated"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


更新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" />
Run Code Online (Sandbox Code Playgroud)

立即更新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" />
Run Code Online (Sandbox Code Playgroud)

  • @CodingJohn 这个项目是开源的,你可以在这里找到 https://github.com/jeromevdl/android-holo-colors (2认同)

Mla*_*jac 181

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

<android.support.v7.widget.AppCompatEditText

      app:backgroundTint="@color/blue_gray_light" />
Run Code Online (Sandbox Code Playgroud)

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

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

  • 您不需要,如果您使用appcompat库,EdtiTexts会自动转换为AppCompatEditText并应用背景色调. (6认同)
  • 这是最好的解决方案!简单,适用于所有API级别.谢谢! (3认同)
  • 可以通过编程设置app:backgroundTint吗?我可以找到方法“ setBackgroundTint” (2认同)

小智 71

您还可以通过对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" />
Run Code Online (Sandbox Code Playgroud)

  • 仅适用于API 21或更高版本.有什么办法让这个工作适用于较低的API级别? (18认同)
  • 对于较小的api级别,仅使用"backgroundTint"而不是"android:backgroundTint"Credits /sf/answers/2058049801/ (3认同)

doc*_*ram 20

以编程方式,您可以尝试:

editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)

  • Xamarin.Forms效果的最佳答案 (2认同)

Rah*_*hul 17

对于21以下的api,您可以使用edittext中的theme属性将下面的代码放入样式文件中

<style name="MyEditTextTheme">
    <item name="colorControlNormal">#FFFFFF</item>
    <item name="colorControlActivated">#FFFFFF</item>
    <item name="colorControlHighlight">#FFFFFF</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在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" />
Run Code Online (Sandbox Code Playgroud)


j2e*_*nue 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"/>
Run Code Online (Sandbox Code Playgroud)

  • 这对带有样式标签的我不起作用,但带有 android:theme 标签 (2认同)

mat*_*ani 10

您可以使用以下代码行以编程方式更改 EditText 的颜色:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));


小智 9

要更改Edittext的下划线颜色:

如果您希望整个应用程序共享此样式,则可以按以下方式执行.

(1)转到styles.xml文件.继承Theme.AppCompat.Light.DarkActionBar(在我的例子中)的父级的AppTheme将是应用程序中所有样式文件的基本父级.将其名称更改为"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>
Run Code Online (Sandbox Code Playgroud)

然后将"colorAccent"更改为您希望EditText线颜色的颜色.

(2)如果你有style.xml的其他值文件夹,这一步非常重要.因为该文件将继承您以前的父xml文件.例如,我有值-19/styles.xml.这是专门针对Kitkat及以上的.将其父级更改为AppBaseTheme并确保删除"colorAccent",以便它不会覆盖父级的颜色.此外,您需要保留特定于版本19的项目.然后它将如下所示.

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)


Kri*_*lim 9

它非常简单(必需:最低API 21)......

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

在此输入图像描述

在此输入图像描述

保持编码........ :)

  • 更改背景色调会更改背景。这个答案不正确 (3认同)
  • 最低API 21. (2认同)

Ego*_*uba 6

线条的颜色由EditText背景属性定义.要更改它,您应该更改android:background布局文件.

我应该注意到这种风格是通过使用9-patch drawable来实现的.如果您查看SDK,可以看到EditText该图像的背景是:

textfield_activated_holo_light.9.png

要更改它,您可以在图像处理程序中打开它并以所需的颜色着色.保存为bg_edit_text.9.png,然后将其放在可绘制的文件夹中.现在您可以将它作为背景应用于您的EditText喜好:

android:background="@drawable/bg_edit_text"
Run Code Online (Sandbox Code Playgroud)

  • 是的,它有效!非常感谢你! (2认同)

Ket*_*ani 5

drawable/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>
Run Code Online (Sandbox Code Playgroud)

设置为编辑文本

<android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_edittext"/>
Run Code Online (Sandbox Code Playgroud)


小智 5

在 xml 布局中使用:

android:backgroundTint="@color/colorPrimary"
Run Code Online (Sandbox Code Playgroud)

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

public void changeLineColorInEditText(EditText editText, int color) {
        editText.setBackgroundTintList(ColorStateList.valueOf(color));
    }
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
Run Code Online (Sandbox Code Playgroud)