TextInputLayout :How to give padding or margin to hint?

后端 未结 15 1860
离开以前
离开以前 2020-11-28 06:42

I have to use TextInputLayout of design support library in my project. I want to give space between hint and EditText in TextInp

相关标签:
15条回答
  • 2020-11-28 07:35

    just use padding-bottom

    <com.google.android.material.textfield.TextInputLayout
    
    
    
                 app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    >
    
                    <com.google.android.material.textfield.TextInputEditText
    
                       ....
                        android:paddingBottom="@dimen/dp_25"
    ...
                       />
                </com.google.android.material.textfield.TextInputLayout>
    
    0 讨论(0)
  • 2020-11-28 07:41

    I tried the padding changes, but it doesn't work well.

    A simple workaround is to change the height of the TIL:

    android:layout_height="wrap_content"
    

    to (e.g.)

    android:layout_height="50dp"
    

    it might not give the same result on all screen sizes.

    And add

    android:gravity="bottom" 
    

    to push your text down.

    0 讨论(0)
  • 2020-11-28 07:42

    The solution proposed by ganesh2shiv works for the most part, although I've found it also de-centres the hint text displayed inside the EditText when not focused.

    A better trick is to set the desired paddingTop to the EditText but also embed the extra padding within the EditText's background. A fairly sane way to do this is to wrap your original background in a <layer-list> and set the <item android:top="..."> attribute to match the paddingTop of your EditText.

    <android.support.design.widget.TextInputLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    
      <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/floating_hint_margin"
        android:background="@drawable/bg_edit_text" />
    </android.support.design.widget.TextInputLayout>
    

    And the bg_edit_text.xml drawable file:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:top="@dimen/floating_hint_margin">
        <your original background; can be <bitmap> or <shape> or whatever./>
      </item>
    </layer-list>
    
    0 讨论(0)
提交回复
热议问题