我如何隐藏EditText下划线(提示行与小衬线在结束)?

可能有更好的方法来做我想做的事情:我有一个带有EditText的布局。通常情况下,在用户可以点击它并开始输入或编辑文本的地方显示正常。

但是,有时我想使用相同的布局(简化其他逻辑)以只读方式显示相同的数据。我希望演示是类似的-它应该有相同的高度和相同的字体,但没有下划线。

作为一种权宜之计,我将通过删除EditText并替换TextView来实现这一点。我认为这将带来预期的结果,但它似乎是一种迂回的昂贵的方式来完成一些本应该通过改变属性来轻松完成的事情。


当前回答

你可以通过编程方式使用setBackgroundResource:

editText.setBackgroundResource(android.R.color.transparent);

其他回答

简单使用

 editText.setBackgroundColor(Color.TRANSPARENT);

我所做的是创建一个Shape drawable,并将其设置为背景:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <padding
        android:top="8dp"
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp" />

    <solid android:color="#fff" />

</shape>

注意:我实际上使用的是@dimen和@color值,但为了清晰起见,我简化了形状文件。

android:background="@android:color/transparent"

Or

android:background="@null"

还可以为editText定义一个STYLE,这样就可以重新组合所有属性。这是非常强大的,如果你有多个编辑文本,需要有行为

将代码放在res/values/styles.xml中

<样式 <项目=“android:背景”>@color/透明</项目> //NO UNDERBAR < name = " android项目:maxLines " > 1 < - >项目 <项目=“android:height”>28dp</项目> //COMMON height < / style >

之后,您只需要在editText中调用它

<EditText
    android:id="@+id/edit1"
    style="@style/MyEditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/edit2"
    style="@style/MyEditTextStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

使用任一属性:

android:background="@null"

OR

android:background="@android:color/transparent"

工作为我隐藏EditText的下划线。

但是,请注意,它会导致TextInputLayout的间距问题,我已经围绕着EditText