如何在 Android 的 EditText 中将提示文本居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11303043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:49:51  来源:igfitidea点击:

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

android

提问by Code Droid

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

我需要EditText在 Android中将提示文本居中。我该怎么做呢?

回答by kyau

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.

为了使提示文本居中以与 EditText 一起使用,您必须确保定义了 android:ellipsize="start"。我不知道为什么这使它起作用,但确实如此。

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" />

回答by Array

Actually, android:gravity="center_horizontal"creates the centering effect you're looking for. Similarly, you can use android:gravity="start"to put hint text at the beginning of the EditText view, and so on.

实际上,android:gravity="center_horizontal"创建您正在寻找的居中效果。同样,您可以使用android:gravity="start"将提示文本放在 EditText 视图的开头,依此类推。

回答by Ankitkumar Makwana

Use this xml attribute:

使用这个 xml 属性:

android:gravity="center"

机器人:重力=“中心”

回答by Amr Angry

use attribute

使用属性

android:gravity="center"

回答by Robi Kumar Tomar

I used this code in every circumstances, and it works perfectly without using android:ellipsize="start"to make a hint in center.

我在任何情况下都使用了这段代码,它在不使用android:ellipsize="start" 的情况下完美地工作。

  <EditText
    android:id="@+id/player2Name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:hint="robi"
    android:inputType="text" />

回答by Abhishek Mathur

I think the answer should be :

我认为答案应该是:

android:textAlignment="center"

回答by DrNachtschatten

textAlignment worked for me.

textAlignment 为我工作。

textAlignment="center"

回答by Cool Soft

Unfortunately, neither answer helped me aligning hint, written in LTR language, while the layout orientation was RTL. I needed such layout in a kind of translation application to make overlay icons not interfere with RTL text. But this trick didn't work with hints while there was no any text yet (icons appeared above the hint) and I came to the following java solution to layout problem:

不幸的是,这两个答案都没有帮助我对齐用 LTR 语言编写的提示,而布局方向是 RTL。我需要在一种翻译应用程序中进行这样的布局,以使覆盖图标不会干扰 RTL 文本。但是这个技巧不适用于提示,而还没有任何文本(提示上方出现图标),我来到了以下布局问题的java解决方案:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
. . .
        if (isRightToLeft()){
            EditText guess = (EditText) rootView.findViewById(android.R.id.text1);
            CharSequence hint = "\u200F" + guess.getHint();
            guess.setHint(hint);
        }
. . .
}

The trick was in right-to-left mark to prepend a left-to-right string. And it seems to work, but does anyone know a more elegant solution?

诀窍是在从右到左的标记中添加从左到右的字符串。它似乎有效,但有人知道更优雅的解决方案吗?

回答by Ishimwe Aubain Consolateur

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="ENTER PIN"
            android:inputType="numberPassword" />
    </android.support.design.widget.TextInputLayout>

Note: in the tag TextInputEditText,the property

注意:在标签TextInputEditText中,属性

android:gravity="center"

机器人:重力=“中心”

is what makes the deal of aligning the text in the center including the hint text

是什么使文本在中心对齐,包括提示文本

回答by Raj Bedi

use this: android:gravity="center"

使用这个: android:gravity="center"