Bonjour,
Comment afficher une table de lignes et colonnes (graphiquement) à partir d'un tableau de valeurs sous Android?
Version imprimable
Bonjour,
Comment afficher une table de lignes et colonnes (graphiquement) à partir d'un tableau de valeurs sous Android?
Apparement c'est un TableLayout.
A quoi sert ces paramètres:
?Code:
1
2
3
4 android:collapseColumns android:shrinkColumns android:stretchColumns
je recherche comment faire un quadrillage pour mon tableau?
Bonjour,
Est ce que ton tableau est un tableau figé ou dynamique ?
C'est-à-dire est ce que le nombre de ligne ou colonne resteront les mêmes ou peuvent elles évoluer ?
elles peuvent évoluer.
Mais je voudrais d'abord afficher un tableau avec un nombre de colonne et lignes connu
le TableLayout peut être utile si ton tableau est fixe.
Si celui ci devient dynamique il va falloir en plus que tu gères l'espace de l'écran , car le fait d'ajouter de nouvelle ligne ou colonne pourra te poser des problèmes.
Tu peux également te créer une vue personnalisé ou tu pourras dans le onDraw de celle ci,te créer directement ton tableau. Tu auras comme cela la largeur et la hauteur de la vue , et ainsi tu pourras définir la hauteur et la largeur de tes cases.
Savez vous comment réaliser le quadrillage du tableau?
Citation:
Savez vous comment réaliser le quadrillage du tableau?
Tu veux savoir dans la vue personnalisé ou dans un TableLayout ?
Dans un TableLayout déclaré dans le code java.
Un exemple :
NB : les couleurs sont flashy :mouarf:Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*" android:background="#ff0000"> <TableRow android:background="#00ff00" android:layout_margin="2dip"> <TextView android:text="label1" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:text="label2" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:text="label3" android:background="#0000ff" android:layout_margin="2dip"/> </TableRow> <TableRow android:background="#00ff00" android:layout_margin="2dip"> <TextView android:text="label4" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:text="label5" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:text="label6" android:background="#0000ff" android:layout_margin="2dip"/> </TableRow> </TableLayout>
oui mais dans le code java ?
Dans le code java
si tu veux ajouter des lignes à ton tableau tu peux faire comme ca
Code:
1
2
3
4
5
6
7
8
9
10
11
12 TableLayout table = (TableLayout) findViewById(R.id.table); TableRow trow=new TableRow(this); TextView tview=new TextView(this); ... trow.addView(tview); .... table.addView(trow);
et pour mettre le quadrillage tu pe utiliser les 2 fonctions suivants
Code:
1
2
3
4 setLayoutParams setsetBackgroundColor
puis je avoir un exemple?
Bonjour,
Si tu sais que ton tableau est fixe, alors pourquoi t'embêter à le faire dans le code :roll: ?Citation:
oui mais dans le code java ?
medi88, t'as déjà donnée un exemple, tu n'as plus qu'a regroupé ce que j'ai posté avec ce que ta proposé medi.Citation:
puis je avoir un exemple?
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 TableLayout tableData = (TableLayout) findViewById(R.id.tabledata); TableRow.LayoutParams tableRowParams =new TableRow.LayoutParams(2); int leftMargin = 10; int topMargin = 10; int rightMargin = 10; int bottomMargin = 20; tableRowParams.setMargins(leftMargin, topMargin, rightMargin,bottomMargin); tablerow = new TableRow(this); tablerow.setLayoutParams(tableRowParams); TextView text = new TextView(this); text.setBackgroundColor(Color.CYAN); tablerow.addView(text); tableData.addView(tablerow,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
Mais cela ne fonctionne pas.
--------------fichier xml ---------
Code:
1
2
3
4
5
6 <TableLayout android:id="@+id/tabledata" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*" android:background="#B9121B"> </TableLayout>
C'est-à-dire ? As tu une erreur ? Quelque chose s'affiche ? ...Citation:
Mais cela ne fonctionne pas.
je n'ai pas les bordures.
Dans mon exemple
Il y a aussi les margin sur les TextView ;).Code:<TextView android:text="label1" android:background="#0000ff" android:layout_margin="2dip"/>
Voilà ce que j'ai fait mais là fond noir, rien ne s'affiche tableau non afficher:
Code:
1
2
3
4 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); llp.setMargins(2, 2, 2, 2); text.setLayoutParams(llp);
J'avais même pas fais gaffe mais tu affiches quelque chose dans ta TextView au fait ?
en fait j'avais le meme probleme que toi
le tableau est deja existant alors que tu affiche un champs qui n'a pas le meme taille que les champs deja existant , le champs ne s'affiche pas
la solution : dans setLayoutParams , tu peux appelé les champs initial de tableau et fait tview.setLayoutParams(t.getLayoutParams)
oui
Exemple svp, car je ne vois pas.Citation:
la solution : dans setLayoutParams , tu peux appelé les champs initial de tableau et fait tview.setLayoutParams(t.getLayoutParams)
Dans mon layout j'ai ça
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 <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/absen" android:stretchColumns="2"> <TableRow> <TextView android:layout_margin="1dip" android:layout_width="30dip" android:layout_height="20dip" android:text="N°" android:gravity="center" android:textSize="15dip" android:id="@+id/num" android:textColor="@color/bleu" /> <TextView android:layout_margin="1dip" android:text="N° Absence" android:gravity="center" android:layout_height="20dip" android:id="@+id/numAbs" android:layout_width="100dip" android:textSize="15dip" android:textColor="@color/bleu"></TextView> <TextView android:layout_margin="1dip" android:text="Date" android:gravity="center" android:layout_height="20dip" android:id="@+id/date" android:layout_width="wrap_content" android:textSize="15dip" android:textColor="@color/bleu"></TextView> <TextView android:layout_margin="1dip" android:text="Nbre jrs" android:gravity="center" android:layout_height="20dip" android:id="@+id/nbreJr" android:layout_width="70dip" android:textSize="15dip" android:textColor="@color/bleu"></TextView> </TableRow> </TableLayout>
et dans le code je fais ca
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 TableLayout table=(TableLayout)findViewById(R.id.absen); TextView num=(TextView) findViewById(R.id.num); TextView numAbs=(TextView) findViewById(R.id.numAbs); TextView date=(TextView) findViewById(R.id.date); TextView nbreJrs=(TextView) findViewById(R.id.nbreJr); TableRow trow=new TableRow(this); TextView text1=new TextView(this); TextView text2=new TextView(this); TextView text3=new TextView(this); text1.setGravity(Gravity.CENTER_HORIZONTAL); text1.setLayoutParams(nbreJrs.getLayoutParams()); text1.setTextColor(Color.BLACK); trow.addView(text1); text2.setGravity(Gravity.CENTER_HORIZONTAL); text2.setLayoutParams(num.getLayoutParams()); text2.setTextColor(Color.BLACK); trow.addView(text1); text3.setGravity(Gravity.CENTER_HORIZONTAL); text3.setLayoutParams(numAbs.getLayoutParams()); text3.setTextColor(Color.BLACK); trow.addView(text1); text4.setGravity(Gravity.CENTER_HORIZONTAL); text4.setLayoutParams(date.getLayoutParams()); text4.setTextColor(Color.BLACK); trow.addView(text1); table.addView(trow)
et si tu veux ajouter plusieurs ligne tu fais une boucle bien sûr
j'espère que c'est utile
S'il n' y a pas de TextView dans le fichier xml; comment faire pour récupérer les paramètres ?
Citation:
text1.setLayoutParams(nbreJrs.getLayoutParams());
Tu veux dire que le tableau est vide dans xml
J'ai jamais essayé ca mais je pense que ca sera difficile car tablelayout a une taille initial et a chaque fois tu change de taille , le tableau ne s'affiche pas
ce que je propose c"est d'ajouter des champs au tableau
ou de creer le tableau manuellement dans le code
J'ai une autre idée a tester
si tu utilise strechcolumn dans XML et dans le code tu ne donne pas de layoutParam au champs qui est etiré
J'ai une erreur;
J'ai crée mon tableLayout dans mon java et mis ce table dans un scrollview, le programme m'indique une erreur d'éxécution mais m'affiche pas l'erreur.
tu peux mettre un try catch et dans les catch e.printstacktrace();
pour afficher l'erreur
+
tu mets ton table directement dans scroll
je pense tu dois mettre un linearlayout dans scroll
et dans linearlayout tu mets ton table
aucun changement , tjs pas les bordures
le printTrace m'indique:
Citation:
java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
c'est ce que j'ai dit
scoll n'accepte pas que tu mets des view a l'interieur
alors tu dois ajouté un linearLayout dans scrollView
et tous le traitement sera dans le linearLayout
Comment créer un LinearLayout et mettre ma tableLayout?
Comme ça :
Code:
1
2
3
4
5
6 LinearLayout lnn=new LinearLayout(); lnn.addView(table); scrool.add(lnn);
Rien ne s'affiche, Fond noir
tu peux creer le layout dans xml
et le table dans le code
voici les lignes qui provoque le fond noir:
Code:
1
2
3
4 LinearLayout.LayoutParams linearText = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); linearText.setMargins(2, 2, 2, 2); text.setLayoutParams(linearText);
setMargin n'a aucun effet sur les elements;
par contre setpadding sur ma tablerow m'affiche les lignes horizontaux de couleur du fond du tablelayout.