2012-05-25 6 views
1

J'ai une linearlayout verticale, elle a à l'intérieur deux linearlayout avec différents éléments, je voudrais fixer le premier au dessus, et le second centré, je l'essaye mais ne fonctionne pas:position de mise en page relative

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

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background_bg" 
    > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:background="#3b5998" 
    >  
     elements 
    </LinearLayout> 


    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="300dip" 
     android:layout_height="300dip" 
     android:background="@drawable/background_resto" 
     android:gravity="center" 
    > 

      elements 
    </LinearLayout> 
    </LinearLayout 

Pourquoi cela ne fonctionne-t-il pas? Nous vous remercions à l'avance

Répondre

3

Je conseille de choisir un RelativeLayout à cette fin.

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/holo_blue_bright" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:layout_alignParentTop="true" 
     android:background="#fff" > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="300dip" 
     android:layout_height="300dip" 
     android:layout_centerInParent="true" 
     android:background="@android:color/holo_green_light" 
     android:orientation="vertical" > 
    </LinearLayout> 
</RelativeLayout> 

Cela vous aidera à définir la position exacte "relative" par rapport à d'autres points de vue.

Meilleurs voeux, Tim

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/background_bg" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="50dip" 
      android:layout_alignParentTop="true" 
      android:background="#ff0000" > 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="300dip" 
      android:layout_height="300dip" 
      android:layout_centerInParent="true" 
      android:background="#ff0000" 
      android:orientation="vertical" > 
     </LinearLayout> 
    </RelativeLayout> 

</LinearLayout> 
+0

En fait celui-ci est même faux. Vous centrez les deux LinearLayouts au milieu du RelativeLayout (c'est-à-dire en les enveloppant tous les deux). Le premier doit être aligné sur le sommet du parent et l'autre sur la position centrale. – Tim

+0

ohhk c'est erreur typo et juste besoin de bon sens pour le rendre correct ... Je pense que vous avez? droite? Erreur de typo –

+0

corrigé maintenant ...... –

2

devrait utiliser RelativeLayout au lieu de LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background_bg" 
    > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:background="#3b5998" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     > 
     elements 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="300dip" 
     android:layout_height="300dip" 
     android:background="@drawable/background_resto" 
     android:layout_centerInParent="true" 
     > 
     elements 
    </LinearLayout> 

</RelativeLayout> 
Questions connexes