Bonjour,
je tente désespérément de créer une ListView personnalisée qui reprend le principe d'une conversation (du type d'une application de sms : l'affichage peut être à droite ou à gauche).
Ma vue qui doit se déplacer est un RelativeLayout, qui doit prendre 70% de la place de l'écran.
J'ai donc créer un LinearLayout père qui contient mon RelativeLayout (avec un poid de 30) et un LinearLayout (avec un poid de 70 et qui ne sert à rien).
Je pensais pouvoir inverser les deux layout fils en configurant la gravité du panel père à droite ou à gauche en fonction du besoin comme indiqué sur la doc :
Sauf que j'ai du mal comprendre car ça ne fonctionne pas...Citation:
You can also specify gravity, which specifies the alignment of all the child elements
Auriez vous une idée pour que ça fonctionne tout en gardant mon ratio de 70% ? (J'aurais pu définir une taille fixe et ne pas mettre de second Layout (celui qui ne sert à rien) puis changer la gravité mais du coup, je perd le pourcentage :( )
Voici le XML pour un élément de ma ListView :
Rendu : Pièce jointe 150731Code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:orientation="horizontal" tools:ignore="UselessLeaf" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" > <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:singleLine="true" android:textSize="24sp" /> <TextView android:id="@+id/dateCreation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@id/label" android:layout_below="@id/label" android:singleLine="true" /> <TextView android:id="@+id/montantTransaction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@id/label" android:singleLine="true" android:textSize="24sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff000000" android:layout_weight="7" /> </LinearLayout>
et le code de mon adapter :
Et dernière petite question : Pourquoi dois-je mettre le poids à 30 si je veux que ce soit à 70% de la taille de l'écran ? ça ne devrait pas être l'inverse ? (70 pour 70 :weird: )Code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package fr.binou.alphaloan.interfaces; import java.util.ArrayList; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; import fr.binou.alphaloan.R; import fr.binou.alphaloan.modeles.Nature; import fr.binou.alphaloan.modeles.Transaction; public class ListViewTransactions extends ArrayAdapter<Transaction>{ private LayoutInflater inflater; public ListViewTransactions(Context context, ArrayList<Transaction> transactions){ super(context, R.layout.transaction_adapter, transactions); inflater = LayoutInflater.from(context); } public View getView(int position, View convertView, ViewGroup parent){ LinearLayout layoutItem; if(convertView ==null){ layoutItem = (LinearLayout) inflater.inflate(R.layout.transaction_adapter, parent, false); } else{ layoutItem = (LinearLayout) convertView; } TextView label = (TextView) layoutItem.findViewById(R.id.label); TextView montant = (TextView) layoutItem.findViewById(R.id.montantTransaction); TextView date = (TextView) layoutItem.findViewById(R.id.dateCreation); Transaction transaction = getItem(position); label.setText(transaction.getLabel()); montant.setText(String.valueOf(transaction.getMontant())); date.setText(transaction.getReadableDateCreation()); if(transaction.getNature() == Nature.ENTRANT) layoutItem.setGravity(Gravity.LEFT); else layoutItem.setGravity(Gravity.RIGHT); return layoutItem; } }
Merci d'avance :mrgreen:
EDIT : Maj de mon code XML + ajout screenshot