How to hide underbar in EditText

后端 未结 25 1768
温柔的废话
温柔的废话 2020-11-28 00:52

How can I hide the EditText underbar (the prompt line with little serifs at the ends)?

There might be a better way to do what I want: I have a layout with an EditTe

相关标签:
25条回答
  • 2020-11-28 01:13

    I discovered the most curious thing! If you set it to null using Data Binding: android:background="@{null}"

    Then not only is the background removed, but the view still has the padding that was calculated from the default background. So for some reason the deferred null setting doesn't clear the padding from the previous bg..? The padding on the view is left/top/right 4dp, bottom 13dp (from emulator level 21).

    May not have same end result on all API levels, so beware! Someone tell me if you test this and find it reliable. (Also note that that bottom padding sticks out because of that underline that was in the original. So you'll probably want to change it in the XML, or reset it in the code after it's loaded to equal top...

    0 讨论(0)
  • 2020-11-28 01:13

    Programmatically use : editText.setBackground(null)

    From xml use: android:background="@null"

    0 讨论(0)
  • 2020-11-28 01:16

    In my case, editText.setBackgroundResource(R.color.transparent); is best.

    It doesn't remove default padding, just under bar.

    R.color.transparent = #00000000

    0 讨论(0)
  • 2020-11-28 01:17

    Using either property:

    android:background="@null"
    

    OR

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

    worked for me to hide the underline of the EditText.

    However, do note that it then causes a spacing issue with the TextInputLayout that I've surrounding the EditText

    0 讨论(0)
  • 2020-11-28 01:19

    Please set your edittext background as

    android:background="#00000000"

    It will work.

    0 讨论(0)
  • 2020-11-28 01:20

    An other option, you can create your own custom EditText like this :

    class CustomEditText : androidx.appcompat.widget.AppCompatEditText {
        constructor(context: Context?) : super(context)
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        private val paint = Paint()
        private val path = Path()
    
        init { // hide your underbar
            this.setBackgroundResource(android.R.color.transparent)
    
            // other init stuff...
        }
    
        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
    
            // draw your canvas...
        }
    }
    
    0 讨论(0)
提交回复
热议问题