Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在RelativeLayout中水平居中编辑文本_Android_Android Edittext_Centering - Fatal编程技术网

Android 在RelativeLayout中水平居中编辑文本

Android 在RelativeLayout中水平居中编辑文本,android,android-edittext,centering,Android,Android Edittext,Centering,我似乎无法将编辑文本水平居中于相对位置。我试过这个: RelativeLayout.LayoutParams editTextParams = ((RelativeLayout.LayoutParams) editText.getLayoutParams()); editTextParams.width = 400; editTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL); editText

我似乎无法将编辑文本水平居中于相对位置。我试过这个:

RelativeLayout.LayoutParams editTextParams = ((RelativeLayout.LayoutParams) editText.getLayoutParams());
        editTextParams.width = 400;
        editTextParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        editText.setLayoutParams(editTextParams);
以下是我的布局XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app"
android:gravity="center_horizontal">
<EditText
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:inputType="textNoSuggestions"
    android:ems="10"
    android:id="@+id/usernameField"
    android:paddingLeft="10dp"
    android:paddingStart="10dp"
    android:paddingRight="0dp"
    android:paddingEnd="0dp"
    android:hint="Enter username"
    android:layout_marginRight="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="16dp" />
</RelativeLayout>

我也试着使用了琐碎的建议

以上这些只是使编辑文本保持左对齐。我希望编辑文本居中

android:layout_centerInParent="true"
将其添加到edittext xml

将此添加到edittext xml中我怀疑

android:paddingStart=“10dp”
android:paddingEnd=“0dp”
正在干扰某个地方

试着把它取下来并告诉我们结果?

我怀疑

android:paddingStart=“10dp”
android:paddingEnd=“0dp”
正在干扰某个地方


尝试删除它并告诉我们结果?

改为在布局中尝试。。。使用{android:layout\u centerHorizontal}。我必须通过编程来完成。请尝试
editTextParams.addRule(父项中的RelativeLayout.CENTER\u,RelativeLayout.TRUE)那没用。谢谢你的努力!发布您的整个布局结构,以便我们提供帮助。在RelativeLayout中有什么?请改为在布局中尝试。。。使用{android:layout\u centerHorizontal}。我必须通过编程来完成。请尝试
editTextParams.addRule(父项中的RelativeLayout.CENTER\u,RelativeLayout.TRUE)那没用。谢谢你的努力!发布您的整个布局结构,以便我们提供帮助。什么是相对论?我必须按程序做。我必须按程序做。