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

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

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

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


当前回答

将background设置为null。

android:background="@null"

其他回答

我发现了一件最奇怪的事!如果使用数据绑定将其设置为null: android:背景= " @{零}"

然后,不仅背景被删除,而且视图仍然有从默认背景计算的填充。所以出于某种原因,延迟空设置不清除填充从以前的bg..?视图上的填充是左/上/右4dp,下13dp(来自模拟器级别21)。

在所有API级别上可能不会有相同的最终结果,所以要小心!谁能告诉我你是否测试过这个方法是否可靠。(还要注意,底部填充突出是因为原来的下划线。所以你可能想要在XML中改变它,或者在代码中重置它,在它加载到equal top…

在您的XML Edittext中使用此代码

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

如下面的代码所示

         <EditText
                    android:id="@+id/EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"/>

您可以将EditText设置为具有自定义的透明绘图对象,或者直接使用

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

or

android:background="@null"

或通过编程的方式

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

还可以为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" />

要同时保留页边距和背景色,请使用:

background.xml

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

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

    <solid android:color="@android:color/transparent" />

</shape>

编辑文本:

<androidx.appcompat.widget.AppCompatEditText
    android:id="@+id/none_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:inputType="text"
    android:text="First Name And Last Name"
    android:textSize="18sp" />