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

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


当前回答

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

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

其他回答

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

使用这个方法..并根据视图名称修改它。这段代码工作得很好。

 private boolean validateMobilenumber() {
            if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) {
                input_layout_mobilenumber.setErrorEnabled(true);
                input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber));
               // requestFocus(mobilenumber);
                return false;
            } else {
                input_layout_mobilenumber.setError(null);
                input_layout_mobilenumber.setErrorEnabled(false);
                mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable());
            }

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

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

保持编码……:)

如果您想要一条平线,可以使用xml轻松地做到这一点。下面是xml示例:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="-1dp"
        android:left="-1dp"
        android:right="-1dp"
        android:bottom="1dp"
        >
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#6A9A3A"/>
        </shape>
    </item>
</layer-list>

如果要为聚焦的编辑文本提供不同的宽度和颜色,则使用选择器替换形状。

更改Edittext的下划线颜色:

如果你想让整个应用程序共享这种风格,那么你可以这样做。

(1)转到styles.xml文件。你的AppTheme继承theme . appcompon . 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行颜色的任何颜色。

如果你有其他style.xml的值文件夹,这一步是非常重要的。因为这个文件将继承你之前的父xml文件。例如,我有values-19/styles.xml。这是专门为奇巧和以上。将它的父类更改为AppBaseTheme,并确保去掉“colorAccent”,这样它就不会覆盖父类的颜色。此外,您还需要保留特定于版本19的项。然后它会是这样的。

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