2013-05-19 2 views
0

Несколько дней назад я искал реализацию edittext с «меткой» внутри для ввода имени пользователя. Я нашел следующий код xml из Stackoverflow.TextView внутри EditText align

<FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"> 

     <EditText 
      android:id="@+id/username_editText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="15sp" 
      android:hint="Πατήστε εδώ" 
      android:inputType="text" 
      android:maxLength="20" 
      android:paddingLeft="125dp" 
      tools:ignore="HardcodedText" > 

      <requestFocus /> 
     </EditText> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginBottom="0dp" 
      android:layout_marginLeft="0dp" 
      android:layout_marginRight="0dp" 
      android:paddingLeft="12dp" 
      android:text="Όνομα χρήστη:" 
      android:textColor="@color/grey" 
      android:textSize="15sp" 
      tools:ignore="HardcodedText" /> 

     </FrameLayout> 

Результатом было то, что мне было нужно, кроме одного thing.Both EditText и TextView не были выровнены по их оси у, в нижней части текста не был приведен в соответствие с небольшой скриншот resolutions.The я получил от 3,7 дюйма : enter image description here

Я рисую красную линию, так как вы можете видеть, что они не выровнены. Любая помощь?

+0

Я знаю, что это не будет выглядеть точно так же, но вы осведомлены о 'андроида: hint' атрибут? http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint – dmon

+0

Да, я знаю это, я использую его уже.TY. btw. – Theodoros80

ответ

3

Для выравнивания текста вам потребуется Relativelayout с параметром макета: android: layout_alignBaseline для вашего компонента. Попробуйте это и держать меня обновление:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" > 

    <EditText 
     android:id="@+id/username_editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:inputType="text" 
     android:ems="10" 
     android:maxLength="20" 
     android:textSize="15sp" 
     android:hint="Πατήστε εδώ" 
     android:paddingLeft="125dp" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/username_editText" 
     android:layout_alignBottom="@+id/username_editText" 
     android:layout_alignParentLeft="true" 
     android:paddingLeft="10dp" 
     android:text="Όνομα χρήστη:" 
     android:text="TextView" /> 

</RelativeLayout> 
+0

Хорошо, я попробую, как только смогу, и я сообщу вам. – Theodoros80

+1

Работал как шарм! – Theodoros80

+0

Хорошо, было приятно помочь вам! – Jarvis

0

Удалить строку:

 android:layout_gravity="center_vertical" 

от вашего TextView. Эта строка делает TextView центрированным по вертикальной оси, тогда как EditText - нет. Вот что вызывает проблему несоосности.

+0

Я тоже попробую и вернусь к вам. – Theodoros80