Jump to content

Positionnement dans un layout


Pierre87

Recommended Posts

C'est un layout utilisé dans une ListView

Je l'ai un poil simplifié pour l'exemple

   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
       android:orientation="vertical"
       android:layout_width="0dip"
       android:layout_height="wrap_content"
       android:layout_weight="1">
           android:id="@+id/text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:layout_margin="5dip"
       android:orientation="vertical">
           android:id="@+id/comment"
           android:layout_width="30dip"
           android:layout_height="30dip" />
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="horizontal">
               android:id="@+id/friends"
               android:layout_width="20dip"
               android:layout_height="20dip"
               android:src="@drawable/lock"
               android:layout_gravity="bottom" />



J'ai donc un linear layout horizontal contenant :

- Un linear layout contenant du texte (sur plusieurs lignes, de taille variable), il prend toute la place disponible

- un linear layout vertical, sa largeur s'adapte à son contenu, dans lequel je veux placer :

- une image en haut

- une image en bas

Le problème, c'est que je n'arrive pas à avoir une image en haut, et l'autre en bas.

Soit j'ai les 2 en haut

Soit elles sont bien placées, mais si le texte est trop petit, les 2 images se chevauchent -.-

Vous avez une idée ? merci !

Link to comment
Share on other sites

En changeant de type de layout?

Exemple avec un RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/text"

android:layout_alignParentTop="true"

android:layout_alignParentLeft="true"

android:layout_alignParentBottom="true"

android:layout_toLeftOf="@id/image_droite" />

android:id="@+id/image_droite"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_margin="5dip"

android:orientation="vertical"

android:layout_alignParentRight="true" >

android:id="@+id/comment"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:gravity="bottom">

android:id="@+id/friends"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/lock" />

Link to comment
Share on other sites

Je pense que tu abuses largement des layout.. Tu en a fais de trop.

Essaye un LL horizontal qui contient un textview et un LL vertical.

Le second LL vertical contient lui 2 images de gravité top et bottom.

il faut simplifier au maximum ton xml.

Edit:

En fait ce que je dis est exactement la meme chose qu'axel, sauf que tu remplaces son relative layout par un linear layout!

Link to comment
Share on other sites

ouais, mais c'est dejà plus ou moins ce que j'ai

les LinearLayout en trop, c'est parce que j'ai au moins 5 autres éléments en plus :P

je vais essayer ton truc Profete, mais je crois que c'etait ce que je faisais lors de mon premier essai

et ça me "collait" mes 2 images en haut :/

j'ai l'impression que sur un LinearLayout vertical, on ne peut influer que sur le positionnement droite/gauche (horizontal) des éléments...

je me trompe ?

Link to comment
Share on other sites

voilà à quoi ressemble mon LinearLayout situé à droite

je n'ai pas vraiment 2 images, mais c'est pareil :P

       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="5dip"
       android:orientation="vertical">
           android:id="@+id/comment"
           android:layout_width="30dip"
           android:layout_height="30dip"
           android:layout_gravity="top">
               android:id="@+id/comment_icon"
               android:layout_width="30dip"
               android:layout_height="30dip" />
               android:id="@+id/comment_count"
               android:layout_width="30dip"
               android:layout_height="22dip"
               android:textColor="#fff"
               android:gravity="center" />

           android:id="@+id/friends_only"
           android:layout_width="20dip"
           android:layout_height="20dip"
           android:src="@drawable/lock"
           android:layout_gravity="bottom" />

tous les Layout "au dessus" on un "layout_height" à "wrap_content"

je me doute que c'est à cause de ça que ça ne marche pas

mais si je les mets à "fill_parent", j'ai des chevauchements dans mes layouts :'(

je sais que c'est un peu moche pour le FrameLayout, mais c'est temporaire :D

Link to comment
Share on other sites

Je suis d'avis que tu pourras simplifier ça en restant sur des LinearLayout et en jouant sur les layout_weight pour "caler" les éléments.

Genre :

horizontal
+-----------------+---------------+
|                 |vertical       |
|                 |+-------------+|
|                 || weight = 1  ||
|                 |+-------------+|
|                 || weight = 0  ||
|                 ||             ||
|                 ||             ||
|                 |+-------------+|
|                 || weight = 1  ||
|                 |+-------------+|
+-----------------+---------------+

Ça ferait 2 LineraLayout imbriqués pour la mise en forme, et le reste c'est tes éléments actifs, donc ça ne me semble pas énorme pour une UI...

Edited by naholyr
Link to comment
Share on other sites

au final j'ai ça :

       android:layout_width="wrap_content"
       android:layout_height="fill_parent"      <------ paramètre qui pose problème
       android:layout_margin="5dip"
       android:orientation="vertical">
           android:id="@+id/comment"
           android:layout_width="30dip"
           android:layout_height="30dip">
               android:id="@+id/comment_icon"
               android:layout_width="30dip"
               android:layout_height="30dip" />
               android:id="@+id/comment_count"
               android:layout_width="30dip"
               android:layout_height="22dip"
               android:textColor="#fff"
               android:gravity="center" />

           android:layout_width="wrap_content"
           android:layout_height="0dip"
           android:layout_weight="1" />
           android:id="@+id/friends_only"
           android:layout_width="20dip"
           android:layout_height="20dip"
           android:src="@drawable/lock" />

je rappelle que c'est juste une partie du layout d'une ligne dans une ListView

ça marche, mais pas toujours :D

tout dépend du "android:layout_height" du LinearLayout (celui le plus à l'extérieur dans mon code)

si je suis en "wrap_content", ça ne va pas, le LinearLayout au milieu ne peut pas s'étendre, et tout est collé en haut

si je suis en "fill_parent", c'est ok dans la plupart des cas, sauf si le reste du contenu de ma ligne est moins haut que le LinearLayout

en gros, j'ai mon FrameLayout qui vient "écraser" mon ImageView

Je crois que mon problème est insoluble :'(

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...