Я не могу показаться центром EditText горизонтально в RelativeLayout. Я попытался это:Центр EditText Горизонтально в RelativeLayout
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>
Я также попытался использовать предложение, сделанное тривиальным.
Вышеуказанные вещи просто заставят EditText оставаться влево. Я хочу, чтобы EditText был центрирован.
Попробуйте в макете вместо этого ... используя {android: layout_centerHorizontal}. – Wizard
Я должен сделать это программно. – rpurohit
Попробуйте 'editTextParams.addRule (RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);' – Wizard