我在我的布局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>

其他回答

如果您想要一条平线,可以使用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的颜色,只需使用以下代码行:

尝试下面的方法,它将转换EditText的底线颜色,当用作背景属性时。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="@dimen/spacing_neg"
        android:right="@dimen/spacing_neg"
        android:top="@dimen/spacing_neg">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="@dimen/spacing_1"
                android:color="@android:color/black" />
        </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>

小部件的后台依赖于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相同的外观和感觉。