我似乎无法在RelativeLayout中水平居中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>
我也尝试过使用Trivial提出的建议。
以上事情只会使EditText保持左对齐。我希望EditText居中。
答案 0 :(得分:0)
android:layout_centerInParent="true"
将其添加到edittext xml
答案 1 :(得分:0)
我怀疑
android:paddingStart="10dp"
和android:paddingEnd="0dp"
在某处干扰。
尝试删除它并告诉我们结果?