2015-11-18 2 views
1

Я не могу показаться центром 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 был центрирован.

+0

Попробуйте в макете вместо этого ... используя {android: layout_centerHorizontal}. – Wizard

+0

Я должен сделать это программно. – rpurohit

+0

Попробуйте 'editTextParams.addRule (RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);' – Wizard

ответ

0
android:layout_centerInParent="true" 

Добавьте к этому EditText XML

+0

Я должен сделать это программно. – rpurohit

0

Я подозреваю, что

android:paddingStart="10dp" и android:paddingEnd="0dp" вмешивается где-то.

Попробуйте удалить его и сообщить нам результат?

Смежные вопросы