0

J'ai un RelativeLayout dans mon projet Android qui affiche une boîte de dialogue personnalisée simple. J'ai un titre en haut, du texte au milieu et un bouton en bas. Tout ce que j'essaie de faire est de centrer le texte entre le en bas du titre et le en haut du bouton. Lorsqu'il est centré, la distance entre le haut du TextView et le bas du titre doit être la même que la distance entre le bas du TextView et le haut du bouton. Pour ce faire, j'ai placé une disposition d'image dans ma disposition relative et je l'ai placée «au-dessus du bouton» et «au-dessous du titre».Verticalement centre TextView entre 2 éléments existants

Ce que je reçois est la suivante:

enter image description here

C'est ce que je veux atteindre:

enter image description here

Voici mon XML (Note, je mets le texte principal TextView programaticaly).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:gravity="fill" 
    android:background="#FFFFFF"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Title" 
     android:layout_marginTop="10dp" 
     android:textColor="#000000" 
     android:gravity="center" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:textSize="40dp">   
    </TextView> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="bottom" 
     android:layout_marginBottom="10dp" 
     android:layout_alignParentBottom="true" 
     android:textSize="25dp" 
     android:text="Click Me"   
     android:background="@drawable/buttonbk" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp"   
     android:textStyle="bold"> 
    </Button> 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/button" 
     android:layout_below="@id/title" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:gravity="center_vertical|center_horizontal" 
      android:paddingTop="50dp" 
      android:paddingLeft="18dp" 
      android:paddingRight="18dp" 
      android:layout_gravity="center" 
      android:textSize="30dp" 
      android:textColor="#000000">   
     </TextView>  

    </FrameLayout> 

</RelativeLayout> 

Répondre

1

Cela peut être une solution possible à votre problème.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:background="@color/light_blue400" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:text="Title" 
     android:textColor="@color/black" 
     android:textSize="40dp" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/button" 
     android:layout_below="@id/title" 
     android:background="@color/light_blue100" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:paddingLeft="18dp" 
      android:paddingRight="18dp" 
      android:text="Text centered vertically" 
      android:textColor="@color/black" 
      android:textSize="30dp" /> 

    </RelativeLayout> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_gravity="bottom" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:text="Click Me" 
     android:textSize="25dp" 
     android:textStyle="bold" /> 

</RelativeLayout> 

Voici les résultats (j'ai coloré l'arrière-plan pour montrer les différentes zones).

  • blanc: Mise en page principale relative
  • bleu clair 400: Titre TextView
  • bleu 100 lumière: Disposition relative pour TextView centrée

Result

+0

Merci @eldivino, je devais déplacer le code du bouton au-dessus de la mise en page intégrée car il me donnait une erreur. Cependant cela a fonctionné et m'a amené à identifier le problème (comme je comparais votre code à la mienne ligne par ligne) ce qui était une erreur idiote de ma part - j'ai ajouté un padding de 50dp en haut de mon textview (android: paddingTop = "50dp"), j'ai enlevé cela de mon code original et cela fonctionne parfaitement. Merci! – Zippy

0

N'a pas la chance de l'essayer, mais quelque chose comme cela devrait fonctionner:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:fillViewport="true" 
android:gravity="fill"> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:gravity="center" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:text="Title" 
    android:textColor="#000000" 
    android:textSize="40dp"></TextView> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/text" 
    android:layout_below="@id/title" 
    android:background="#FFFFFF"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_marginTop="5dp" 
     android:paddingLeft="18dp" 
     android:paddingRight="18dp" 
     android:paddingTop="50dp" 
     android:textColor="#000000" 
     android:textSize="30dp"></TextView> 
</RelativeLayout> 

<Button 
    android:id="@+id/button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_gravity="bottom" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:background="@drawable/buttonbk" 
    android:text="Click Me" 
    android:textSize="25dp" 
    android:textStyle="bold"></Button> 
</RelativeLayout> 
0

essayer cette

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:fillViewport="true" 
android:gravity="fill" 
android:background="#FFFFFF"> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Title" 
    android:layout_marginTop="10dp" 
    android:textColor="#000000" 
    android:gravity="center" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:textSize="40dp">   
</TextView> 

<Button 
    android:id="@+id/button" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_gravity="bottom" 
    android:layout_marginBottom="10dp" 
    android:layout_alignParentBottom="true" 
    android:textSize="25dp" 
    android:text="Click Me"   
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp"   
    android:textStyle="bold"> 
</Button> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/button" 
    android:layout_below="@id/title" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:paddingLeft="18dp" 
     android:paddingRight="18dp" 
     android:layout_gravity="center" 
     android:textSize="30dp" 
     android:text="Hi Im textView" 
     android:textColor="#000000">   
    </TextView>  

</FrameLayout>