2017-10-16 22 views
-1

Dans le cas d'une disposition linéaire verticale, je souhaite aligner le centre de texte verticalement.Alignement dans la disposition linéaire - Android

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="sample1" 
    android:layout_gravity="center" 
    android:gravity="center"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="sample2" 
    android:layout_marginTop="150dp"/> 

Je voudrais avoir "Sample1" au centre verticalement, NOTE: layout_gravity = "center" fournit simplement un alignement par le centre horizontal et non vertical centré.

Référez Image

Répondre

0

essayer.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:text="sample1"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:text="sample2"/> 
</LinearLayout> 
+0

Works, merci ..! –

0

Eh bien si vous voulez aller avec la mise en page relative. Essayez cette façon, il travaillera pour vous

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:id="@+id/sample1" 
     android:layout_centerVertical="true" 
     android:text="sample1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_below="@+id/sample1" 
     android:text="sample2"/> 
</RelativeLayout> 

SORTIE

enter image description here