首页 文章

如何在EditText中隐藏下划线

提问于
浏览
347

如何隐藏EditText下划线(两端带有小衬线的提示行)?

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

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

作为一种权宜之计,我将通过删除EditText并替换TextView来实现这一点 . 我认为这会产生预期的结果,但似乎是一种迂回的做法,通过改变属性来做一些应该很容易做到的事情 .

16 回答

  • 12

    我做的是创建一个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 值作为firelds,但为了清楚起见,我在这里简化了形状文件 .

  • 9

    将背景设置为null .

    android:background="@null"
    
  • 91

    您可以使用 setBackgroundResource 以编程方式执行此操作:

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

    您还可以为editText定义STYLE,以便重新组合所有属性 . 如果您需要多个编辑需要具有该行为的文本,则它非常强大

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

    <style name =“MyEditTextStyle”parent =“@ android:style / TextAppearance.Widget.EditText”>

    <item name =“android:background”> @ color / transparence </ item> // NO UNDERBAR
    <item name =“android:maxLines”> 1 </ item>
    <item name =“android:height”> 28dp </ item> //常见高度
    </样式>

    之后,您只需要在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" />
    
  • 3

    使用任一属性:

    android:background="@null"
    

    要么

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

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

    但是,请注意它然后导致与我围绕 EditText 的TextInputLayout的间距问题

  • 755

    这是一种隐藏它的方法,而不会破坏默认填充:

    fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
        val paddingBottom = this.paddingBottom
        val paddingStart = ViewCompat.getPaddingStart(this)
        val paddingEnd = ViewCompat.getPaddingEnd(this)
        val paddingTop = this.paddingTop
        ViewCompat.setBackground(this, background)
        ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
    }
    

    用法:

    editText.setViewBackgroundWithoutResettingPadding(null)
    
  • 4

    我发现了最奇怪的事情!如果使用数据绑定将其设置为 nullandroid:background="@{null}"

    然后不仅删除了背景,而且视图仍然具有从默认背景计算的填充 . 因此,由于某种原因,延迟空值设置不会清除前一个bg的填充..?视图上的填充是左/上/右4dp,下13dp(来自仿真器级别21) .

    所有API级别可能没有相同的最终结果,所以要小心!有人告诉我,如果你测试这个并发现它可靠 . (另请注意,底部填充因为原始下划线而突出 . 因此,您可能希望在XML中更改它,或者在加载到顶部之后在代码中重置它...

  • 5

    我有这样的东西是非常有用的:

    generalEditText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
    

    其中generalEditText是我的EditText,颜色为白色:

    <color name="white">#ffffff</color>
    

    这不会删除填充,您的EditText将保持原样 . 只会删除底部的行 . 有时像这样做更有用 .

    如果您使用 android:background="@null" 建议您丢失填充并且EditText变小 . 至少,这是我的情况 .

    一个小小的注意事项是,如果你设置后台null并尝试我上面提供的java代码,你的应用程序将在执行后立即崩溃 . (因为它得到了背景,但它是空的 . )这可能很明显,但我认为指出它很重要 .

  • 22

    Simply Use This

    editText.setBackgroundColor(Color.TRANSPARENT);
    
  • 0

    您可以将 EditTextbackgroundTint 值设置为特定颜色 . 如果设置透明色,则下划线应该消失 .

    android:backgroundTint="@color/Transparent"

    <color name="Transparent">#00000000</color>

    但是你可以在 Api v21(Lollipop) 或更高版本中使用它

  • 2

    以编程方式使用: editText.setBackground(null)

    来自 xml 使用: android:background="@null"

  • 16

    您还必须设置minWidth,否则如果文本为空,光标将消失 .

    <EditText
                android:id="@+id/et_card_view_list_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="30dp"
                android:layout_weight="1"
                android:inputType="text"
                android:text="Name"
                android:background="@android:color/transparent"
                />
    
  • 9

    如果您希望这会影响EditText的所有实例以及从中继承的任何类,那么您应该在主题中设置属性editTextBackground的值 .

    <item name="android:editTextBackground">@drawable/bg_no_underline</item>
    

    我使用的drawable的一个例子是:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
         android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
         android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
         android:insetTop="@dimen/abc_edit_text_inset_top_material"
         android:insetBottom="@dimen/abc_edit_text_inset_bottom_material">
        <selector>
          <item android:drawable="@android:color/transparent"/>
        </selector>
    </inset>
    

    这是默认材料设计实现的略微修改版本 .

    应用后,它将使您的所有EditText删除整个应用程序中的下划线,并且您不必手动将样式应用于每个应用程序 .

  • 24

    您可以将 EditText 设置为具有自定义透明可绘制或仅使用

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

    要么

    android:background="@null"
    
  • 2

    请将您的edittext背景设置为

    android:background="#00000000"

    它会工作 .

  • 1

    如果您的编辑文本已经有背景,那么您可以使用以下内容 .

    android:textCursorDrawable="@null"
    

相关问题