2017-10-07 17 views
0

J'ai un LinearLayout horizontal et un EditText center_horizontal et tout est ok mais quand j'essaye d'ajouter un TextView à son côté gauche (de l'EditText) le EditText se déplace vers la droite car les deux contrôles TextView et EditText sont centrés. Mon problème est que je veux que le EditText reste au centre et le TextView à sa gauche sans déplacer la position centrale de l'EditText. ceci est mon code sans le TextViewImpossible de mettre en œuvre lorsque vous essayez de centrer horizontal edittext dans linearlayout et ajouter un textview sur son côté gauche

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txtTituloFechaAgenda" 
    android:orientation="horizontal" 
    android:weightSum="2" 
    android:id="@+id/ll1agenda" 
    android:background="@color/azul" 

    android:gravity="center_horizontal" 
    > 



    <EditText 
     android:inputType="date" 
     android:ems="10" 
     android:layout_alignParentTop="true" 
     android:id="@+id/etFechaAgenda" 
     style="@style/textoETFecha" 
     android:background="@drawable/style_edit_text1" 
     android:layout_height="37dp" 
     android:gravity="center_horizontal" 

     android:layout_width="150dp" /> 


</LinearLayout> 

jusqu'à là le EditText est centré ok. suivant j'ai ajouté le TextView:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txtTituloFechaAgenda" 
    android:orientation="horizontal" 
    android:weightSum="2" 
    android:id="@+id/ll1agenda" 
    android:background="@color/azul" 

    android:gravity="center_horizontal" 
    > 

    <TextView 
     android:text="Fecha: " 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearLayout" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 

     style="@style/textoTitulosBlanco" 
     android:id="@+id/txtFechaAgenda" /> 

    <EditText 
     android:inputType="date" 
     android:ems="10" 
     android:layout_alignParentTop="true" 
     android:id="@+id/etFechaAgenda" 
     style="@style/textoETFecha" 
     android:background="@drawable/style_edit_text1" 
     android:layout_height="37dp" 
     android:gravity="center_horizontal" 

     android:layout_width="150dp" /> 


</LinearLayout> 

quand je l'ajoute TextView les deux éléments sont centrés et mes EditText se déplace vers la droite et perdre la position centrale. J'espère que vous pouvez m'aider. merci d'avance ...

Répondre

0

Une solution possible à votre problème pourrait être, (ce que j'ai compris de la question) pour ajouter une troisième vue sur le côté droit d'edittext et mettre sa visibilité à invisible. (Afin qu'il ne se montre pas mais prenne l'espace)

+0

Oui, j'utilise ce trik, mais cela doit être correct. merci im en utilisant votre solution pour le moment – matQ

0

Ceux-ci ne fonctionnent pas dans la disposition linéaire. Ils travaillent dans la mise en page relative

android:layout_alignParentLeft="true" 
android:layout_alignParentStart="true" 

Utilisez layout_weight pour organiser editText et TextView

<TextView 
     android:text="Fecha: " 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     style="@style/textoTitulosBlanco" 
     android:id="@+id/txtFechaAgenda" /> 

    <EditText 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/etFechaAgenda" 
     style="@style/textoETFecha" 
     android:background="@drawable/style_edit_text1" 
     android:layout_height="37dp" 
     android:layout_width="0dp" 
     android:layout_weight="2"/> 
0
android:layout_centerHorizontal="true" 

centre Marque vrai horizontal pour le texte d'édition en utilisant la mise en page relative. Essayez le segment de code ci-dessous.

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:padding="4dp" 
      android:text="Fecha:" /> 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:padding="4dp" 
      android:text="Edit Text" /> 

    </RelativeLayout>