How do I center the hint text within an EditText in Android?

后端 未结 14 1469
走了就别回头了
走了就别回头了 2020-12-05 12:54

I need to center the Hint text within an EditText in Android. How do I do this?

相关标签:
14条回答
  • 2020-12-05 13:05

    In order for centering of hint text to work with EditText you have to make sure android:ellipsize="start" is defined. I don't know why this makes it work, but it does.

    Example pulled from personal code:

    <EditText
        android:id="@+id/player2Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ellipsize="start"
        android:gravity="center_horizontal"
        android:hint="@string/player2_name"
        android:inputType="textCapWords|textPersonName"
        android:singleLine="true" />
    
    0 讨论(0)
  • 2020-12-05 13:08

    use android:textAlignment="center" in EditText , it work for me

    0 讨论(0)
  • 2020-12-05 13:08

    My problem was that the EditText wasn't big enought to fit exactly inside its parent (FrameLayout in my case), so using just android:gravity="center" (center_vertical, horizontal, start, or whatever) would just fit it inside the EditText, and not in its parent, so I had to center the EditText inside its parent using:

    android:layout_gravity="center"

    pd: I used android:background="@android:color/transparent" to hide the ugly underline in the EditText hint

    0 讨论(0)
  • 2020-12-05 13:14

    I think the answer should be :

    android:textAlignment="center"
    
    0 讨论(0)
  • 2020-12-05 13:16

    use this: android:gravity="center"

    0 讨论(0)
  • 2020-12-05 13:19

    I use this and worked for me

    android:gravity="Left|center_vertical"
    
    0 讨论(0)
提交回复
热议问题