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

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


当前回答

 <EditText
        android:id="@+id/et_password_tlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:textColorHint="#9e9e9e"
        android:backgroundTint="#000"
        android:singleLine="true"
        android:drawableTint="#FF4081"
        android:paddingTop="25dp"
        android:textColor="#000"
        android:paddingBottom="5dp"
        android:inputType="textPassword"/>

    <View
        android:id="@+id/UnderLine"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/et_password_tlay"
        android:layout_centerHorizontal="true"
        android:background="#03f94e" />

**是视图**的一种操作

其他回答

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

小部件的后台依赖于API级别。

选择1

可以为EditText背景提供自定义图像

android:background="@drawable/custom_editText"

你的图像应该是这样的。它会给你想要的效果。

选择2

将此xml设置为EditText背景属性。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" android:padding="10dp">
<solid android:color="#4C000000"/>
    <corners android:bottomRightRadius="5dp"
             android:bottomLeftRadius="5dp"
             android:topLeftRadius="5dp"
             android:topRightRadius="5dp"/>
</shape>

这将在每个API上具有与EditText相同的外观和感觉。

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

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

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

android:background="@drawable/bg_edit_text"

可拉的/ 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"/>

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

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