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

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


当前回答

更改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>

其他回答

这是最好的工具,你可以使用所有的观点,它的免费许多感谢@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" />

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

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

小部件的后台依赖于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的颜色,只需使用以下代码行:

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