IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Centrer le contenu d'un Linearlayout


Sujet :

Android

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut Centrer le contenu d'un Linearlayout
    Bonjour à tous,

    J'aimerais centrer le contenu d'un LineairLayout sans déformer mon autre LineairLayout.

    Je m'explique j'ai une listView composée d'une image et d'un textView.

    Mes images sont placées à Gauche et mes text à droite des images.

    J'aimerais centrer mon LineairLayout tous en conservant l'alignement vertical des images.

    Voici mon code:

    Ma listView:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?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"
        >
     <ListView
    		        android:id="@+id/listviewperso"
    		      	android:layout_width="match_parent"
            		android:layout_height="wrap_content"
    		        android:layout_centerHorizontal="true"
    				 android:fadeScrollbars="false"
    		         /> 
    </LinearLayout>
    Le contenu de ma listView:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <?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 xmlns:android="http://schemas.android.com/apk/res/android"
    		    android:layout_width="wrap_content"
    		    android:layout_height="wrap_content"
    		    android:orientation="horizontal"
    			android:layout_gravity="center_horizontal"
    		    >
     
    			    <ImageView
    					android:id="@+id/img"
    					android:layout_width="wrap_content"
    					android:layout_height="wrap_content"
    				   	android:padding="10px"
    					/>
     
     
    				    <TextView android:id="@+id/titre"
    				         android:layout_width="wrap_content"
    				         android:layout_height="wrap_content"
    				         android:textSize="16px"
    				         android:textStyle="bold"
    				         android:textColor="#FFF3A2"/>
     
     
    		</LinearLayout> 
    </LinearLayout>
    Et ma méthode onCreate():

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    	  ListView maListViewPerso = (ListView) findViewById(R.id.listviewperso);
    	        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
    	         HashMap<String, String> map;
    	        map = new HashMap<String, String>();
    	        map.put("titre", "Agenda Besançon ce jour"); 
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
    	        map = new HashMap<String, String>();
    	        map.put("titre", "Agenda Besançon ce week-end");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
    	        map = new HashMap<String, String>();
    	        map.put("titre", "Les news");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);      
    	        map = new HashMap<String, String>();
    	        map.put("titre", "Cinéma");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
    	        map = new HashMap<String, String>();
    	        map.put("titre","Informations Commerce");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
    	        map = new HashMap<String, String>();
    	        map.put("titre","Offres d'emplois");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
    	        map = new HashMap<String, String>();
    	        map.put("titre", "Les petites annonces");
    	        map.put("img", String.valueOf(R.drawable.ic_launcher));
    	        listItem.add(map);
     
    	        SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,
    	                new String[] {"titre","img"}, new int[] { R.id.titre,R.id.img});
    	        maListViewPerso.setAdapter(mSchedule);
    Avec ce bous de code j'aligne le contenu de ma listView, mais mes images ne sont plus du tout aligné verticalement.

    Alors, comment puis-je faire?

    Merci d'avance

  2. #2
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    A vue de nez il ya un LinearLayout de trop (vu que l'un d'entre eux ne contient qu'un seul élément).

    Si toutes les images sont de taille fixe...
    Alors un simple TextView suffi:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <TextView android:id="@+id/titre"
    	android:layout_width="match_parent"
    	android:layout_height="wrap_content"
            android:gravity="center"
    	android:textSize="16px"  <<== HOULA c'est PAS BON DU TOUT CA ! (ca fait 1.27mm sur une Nexus 10 ... textSize devrait s'exprimer en 'sp'
            android:textStyle="bold"
            android:textColor="#FFF3A2"/>
    Et utiliser setCompoundDrawables() pour rajouter une image à gauche.

    Sinon un simple LinearLayout horizontal, avec une image à gauche (width=wrap_content) et un textview (width=fill_parent, gravity=center). Mais normalement Lint devrait dire "pas la peine d'utiliser deux views !" et conseiller d'utiliser ma première solution.
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Merci beaucoup de ta réponse Nicroman je vais essayer.

    Tu m'as fait une remarque me disant que mon textSize devait être exprimé en sp.

    Pourquoi sp serait-il mieux que xp? Le sp s'adapte à l'écran?

    Cela est-il aussi valable pour la taille d'un boutton?

    Voici un exemple de comment je déclare mes bouttons:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     <Button android:id="@+id/ButNotification" 
    		        android:layout_height="wrap_content" 
    		        android:layout_width="fill_parent" 
    		           android:layout_below="@+id/ButGps"
    		                android:height="70px"
    		              />
    Merci d'avance

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Alors, j'ai essayé ta 1er méthode, mais je ne suis pas arrivé à la faire fonctionner dans ma listView, j'ai testé avec des textView et ça à l'air d'être la même disposition que moi.

    J'ai donc testé ta 2ème solution en la modifiant un peu afin d'obtenir la disposition que je souhaite.

    Voici le résultat:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	      android:orientation="horizontal"
    	      android:layout_width="wrap_content"
    	      android:layout_height="wrap_content"
     
     
    	      >
     
    		    <ImageView
    			android:id="@+id/img"
    			android:layout_width="wrap_content"
    			android:layout_height="wrap_content"
     
     
    			/>
     
    		  <TextView android:id="@+id/titre"
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    	        android:gravity="center"
     
    	        />
     
    	</LinearLayout>



    La disposition est nickel, mais comment centrer horizontalement le contenu du LinearLayout?

    Merci d'avance

  5. #5
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    http://nicroman.developpez.com/tutor...nites-mesures/

    Le "DP" s'adapte à la taille de l'écran.
    Le "SP" s'adapte à la taille du texte (relatif au DP)

    Le "PX" ben, est fixe (en pixels)... donc entre un Galaxy Ace et un Nexus 10, il va y avoir de sacrées différences.


    Sinon, je ne comprends pas la disposition à laquelle tu veux arriver..
    J'avais compris une ListView
    Des Items dedans.
    Chaque item contient une image + text. Tu veux conserver les images alignées et le texte centré. C'est bien ça ?
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    D'accord je vais voir ça.

    En fait, je voudrais que les images soient alignées, que les textes apparaissent à droit de mes images (Pas forcements aligné). Et que le tous soient centré sans perdre la disposition

  7. #7
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Donc c'est la listview qui est centrée ?
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Oui

Discussions similaires

  1. [CSS] centrer le contenu de <table>
    Par H-bil dans le forum Mise en page CSS
    Réponses: 14
    Dernier message: 10/06/2006, 16h44
  2. [VB6]Comment centrer le contenu d'une cellule
    Par zackrel dans le forum VB 6 et antérieur
    Réponses: 15
    Dernier message: 19/05/2006, 20h02
  3. [HTML] Centrer le contenu d'une zone de texte
    Par beegees dans le forum Balisage (X)HTML et validation W3C
    Réponses: 4
    Dernier message: 16/03/2006, 00h34
  4. [HTML] Centrer le contenu d'une zone de texte
    Par beegees dans le forum Balisage (X)HTML et validation W3C
    Réponses: 13
    Dernier message: 15/03/2006, 17h10
  5. [CSS] Centrer le contenu d'une Div
    Par bolo dans le forum Mise en page CSS
    Réponses: 2
    Dernier message: 09/03/2006, 19h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo