【问题标题】:EditText underline below text property文本属性下方的 EditText 下划线
【发布时间】:2014-02-24 04:00:38
【问题描述】:

我想更改编辑文本下方的蓝色,我不知道它是什么属性。

我尝试为它使用不同的背景颜色,但它不起作用。

我在下面附上了一张图片:

【问题讨论】:

  • 这是 EditText 的背景,可在某些 api 级别/电话上使用。换句话说,如果您的应用在不同的手机上运行,​​您可能会看到不同的背景。
  • 为您创建自定义edittext背景
  • 可能与前一个线程重复。请查看stackoverflow.com/a/4585750/1129468
  • 使用下面的代码让我知道它是否有效。

标签: android login android-edittext


【解决方案1】:

以编程方式设置 EditText 的下划线颜色实际上相当容易(只需一行代码)。

设置颜色:

editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

去除颜色:

editText.getBackground().clearColorFilter();

注意:当EditText开启focus时,你设置的颜色不会生效,而是有一个焦点颜色。

API 参考:

Drawable#setColorFilter

Drawable#clearColorFilter

【讨论】:

  • 也适用于 Xamarin for Android,在您的自定义渲染器 OnElementChanged 中您可以执行 Control.Background.SetColorFilter(Android.Graphics.Color.White, PorterDuff.Mode.SrcIn);
  • @DavidConlisk 你覆盖了哪个Renderer?我正在尝试弄清楚如何使它适用于搜索栏。
  • @NickN。我将它用于选择器,所以我覆盖了 PickerRenderer。它应该适用于最适合您的任何渲染器(理论上!)
  • 我想应该,但问题可能是SearchBarRenderer 不是EditText 类型。无法让它工作。
【解决方案2】:

在您的 EditText xml 布局中使用 android:backgroundTint=""

对于 apiAppCompatEditText thenapp:backgroundTint=""

【讨论】:

  • 目前仅适用于 api 21 及更高版本。没有向后支持。
  • 这是迄今为止最简单、最简洁的解决方案。
  • @JohnShelley 见my answer below,它适用于棒棒糖前及更高版本。
  • android:background="@null" 用于具有 api 的设备
  • 要使这个属性在低于 21 的 API 中工作,你应该使用 app (xmlns:app="schemas.android.com/apk/res-auto") 命名空间而不是 android。
【解决方案3】:

对于EditText 的每个状态(焦点、启用、激活),您必须使用不同的背景图像,而不是颜色。

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

在上面的站点中,您可以从 Holo 主题中的许多组件中获取图像。只需选择“EditText”和您想要的颜色。您可以在页面底部看到预览。

下载 .zip 文件,然后复制粘贴项目中的资源(图像和 XML)。

如果您的 XML 被命名为:apptheme_edit_text_holo_light.xml(或类似名称):

  1. 转到您的 XML“styles.xml”并添加自定义 EditText 样式:

    <style name="EditTextCustomHolo" parent="android:Widget.EditText">
       <item name="android:background">@drawable/apptheme_edit_text_holo_light</item>
       <item name="android:textColor">#ffffff</item>
    </style>
    
  2. 只需在您的 EditText 中执行此操作:

    <EditText
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       style="@style/EditTextCustomHolo"/>
    

就是这样,希望对你有帮助。

【讨论】:

    【解决方案4】:

    这适用于新旧版本的 Android(即使在 API 10 上也能正常工作!)。

    在您的 styles.xml 中定义此样式:

    <style name="EditText.Login" parent="Widget.AppCompat.EditText">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textColorHint">@android:color/darker_gray</item>
        <item name="colorAccent">@color/blue</item>
        <item name="colorControlNormal">@color/blue</item>
        <item name="colorControlActivated">@color/blue</item>
    </style>
    

    现在在您的 XML 中,将其设置为主题和样式(style 设置 textColortheme 设置所有其他内容):

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        style="@style/EditText.Login"
        android:theme="@style/EditText.Login"/>
    

    编辑

    此解决方案会在选择句柄带有下划线的较新 Android 版本(Lollipop 或 Marshmallow 及更高版本)上导致微小的 UI 故障。

    this thread 讨论了此问题。 (我没有亲自尝试过这个解决方案)

    【讨论】:

    • 很好的答案,谢谢。使用 parent="Widget.AppCompat.EditText" 而不是 parent="android:Widget.EditText" 解决了我的主要问题。
    • 我认为你的意思是这适用于 Lollipop (21) 及以上,因为我收到警告说这不低于 21
    • @MarkO'Sullivan 不,这适用于棒棒糖之前的版本以及一直向上。我已经在 API 10 上对其进行了测试,它运行良好。请粘贴完整的错误及其显示位置。
    • 重点编辑文本对我来说仍然是原色,所以我不得不使用这种方法:stackoverflow.com/a/36543554/2413303
    【解决方案5】:

    您可以在 styles.xml 中更改 EditText 颜色的 Underline 并指定它。在您的应用主题 styles.xml 中添加以下内容。

    <item name="android:textColorSecondary">@color/primary_text_color</item> 
    

    正如ana在评论部分指出的那样

      <item name="android:colorControlActivated">@color/black</item>
    

    在主题样式中设置此项非常适合更改编辑文本下划线的颜色。

    【讨论】:

    • 请注意android:textColorSecondary 也用于确定ActionBar 中的后退箭头和DrawerLayout 汉堡图标的颜色。
    • 由于导航栏着色问题,我最终使用了&lt;item name="colorControlNormal"&gt;@color/edittext_underline_color&lt;/item&gt; 而不是 textColorSecondary。
    【解决方案6】:

    因此,您需要在可绘制文件夹中创建一个新的 .xml 文件。

    在该文件中粘贴以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
       <item
        android:bottom="8dp"
        android:left="-3dp"
        android:right="-3dp"
        android:top="-3dp">
        <shape android:shape="rectangle">
         <stroke
           android:width="1dp"
           android:color="@color/white"/>
         </shape>
       </item>
    </layer-list>
    

    在你的 EditText 中,设置

    android:background="@drawable/your_drawable"
    

    您可以使用可绘制的 xml、设置角、填充等。

    【讨论】:

    • 这可能是更好的解决方案,因为它允许完全自定义 EditText,而不影响样式或布局中的其他组件
    • 据我所知,您可以将edittext的背景设置为白色
    • 浏览了我能找到的所有解决方案,这是唯一一个使用 Android Material 组件为我工作的解决方案。
    【解决方案7】:

    在您的应用样式中定义属性colorAccent。在这里你可以找到一个例子

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/action_bar</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/action_bar</item>
    </style>
    

    【讨论】:

    • 这对我来说是最好的答案,但应该稍微增强一点,说在您声明覆盖主主题 (Theme.AppCompat.Light) 的主题 (AppTheme) 后,您必须选择此主题在清单中
    【解决方案8】:

    您可以轻松地使用这行代码以编程方式更改 EditText 的颜色:

    edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));

    【讨论】:

    • 像魅力一样工作:D
    【解决方案9】:

    要更改底线颜色,您可以在应用主题中使用它:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    
        <item name="colorControlNormal">#c5c5c5</item>
        <item name="colorControlActivated">#ffe100</item>
        <item name="colorControlHighlight">#ffe100</item>
    </style>
    

    要更改浮动标签颜色,请编写以下主题:

    <style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
    <item name="android:textColor">#4ffd04[![enter image description here][1]][1]</item>
    </style>
    

    并在您的布局中使用此主题:

     <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout">
    
        <EditText
            android:id="@+id/edtTxtFirstName_CompleteProfileOneActivity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:capitalize="characters"
            android:hint="User Name"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:singleLine="true"
            android:textColor="@android:color/white" />
    
    </android.support.design.widget.TextInputLayout>
    

    【讨论】:

    【解决方案10】:

    使用以下代码更改编辑文本边框的背景颜色。

    在drawable下创建新的XML文件。

    abc.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#00000000" />
        <stroke android:width="1dip" android:color="#ffffff" />
    </shape>
    

    并将其添加为编辑文本的背景

    android:background="@drawable/abc"
    

    【讨论】:

    • 我提到我曾尝试在上面设置背景颜色。它没有用。
    • 不是下划线你的代码是关于边框的。
    【解决方案11】:

    如果您不必支持 API

     <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:hint="Task Name"
                android:ems="10"
                android:id="@+id/task_name"
                android:layout_marginBottom="15dp"
                android:textAlignment="center"
                android:textColor="@android:color/white"
    
                android:textColorLink="@color/blue"
                android:textColorHint="@color/blue"
                android:backgroundTint="@color/lighter_blue" />
    

    为了获得更好的支持和后备,请使用@Akariuz 解决方案。 backgroundHint 是最轻松的解决方案,但不向后兼容,根据您的要求拨打电话。

    【讨论】:

      【解决方案12】:

      更改您的 colorAccent 您需要在 colorAccent 上设置的颜色并运行您得到输出

      【讨论】:

        【解决方案13】:

        只需将 xml 代码中的 android:backgroundTint 更改为您的颜色

        【讨论】:

          【解决方案14】:

          您可以使用 AppCompatEditText 和颜色选择器来做到这一点:

                      <androidx.appcompat.widget.AppCompatEditText
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          app:backgroundTint="@color/selector_edittext_underline" />
          

          selector_edittext_underline.xml:

          <selector xmlns:android="http://schemas.android.com/apk/res/android">
              <item android:color="@color/focused_color" 
                    android:state_focused="true" />
              <item android:color="@color/hint_color" />
          </selector>
          

          注意:将此选择器文件放在res/color 文件夹中,而不是res/drawable。通常res/color 不存在,我们可能需要创建它。

          【讨论】:

          • 这真是没用的答案。按属性,'backgroundTint' 的颜色类型是不可绘制的!这里来自文档&lt;attr format="color" name="backgroundTint"/&gt;。并且那个选择器不是颜色而是drawable
          • 这不是没用的,你必须将这个选择器添加到 res/color 目录中。是的,我明白了,这一定令人困惑,但确实有效。选择器可在 res 内的 color 文件夹中绘制。只有这样,它才会被视为不可绘制的颜色。让我更新这个答案。
          猜你喜欢
          • 1970-01-01
          • 2013-08-22
          • 2013-03-31
          • 1970-01-01
          • 1970-01-01
          • 2011-08-19
          • 1970-01-01
          • 2021-09-15
          • 1970-01-01
          相关资源
          最近更新 更多