如何给 EditText 添加下划线

How to underline an EditText

我在 AndroidStudio 中为 EditText 加下划线时遇到一些问题。

这就是我要找的(这只是一张照片,不是我的真实文字):

但我真的不知道 属性 可以做到这一点。

我现在的代码非常简单。只是“正常”的:

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_margin="16dp"
        android:id="@+id/tx_titulo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="@string/pt_titulo"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:allowUndo="true"
        />

我不想只在文本下划线(在本例中为“Título”),而是整个 PlainText

有人知道吗? 问候。

要以编程方式执行此操作,您可以在此 post 中找到答案。

设置下划线颜色:

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

要删除下划线颜色:

editText.getBackground().clearColorFilter();

要从 XML 执行此操作,您可以添加背景 属性 以在 layout.xml 中使用任何可绘制资源作为背景执行相同的操作,方法是添加:

android:background="@drawable/mydrawable"

如果你有几个具有相同样式配置的 EditText,为了避免为每个 EditText 设置属性,你可以覆盖 EditText 的默认属性,像完成一样定义自定义样式 here.

这是默认设置,但请尝试检查您的样式。也许有一种样式会覆盖整个应用程序中的样式。有的话就去掉