bobby51 Posted March 7, 2012 Share Posted March 7, 2012 Bonjour à tous! voila mon probleme: j'utilise un table layout pour afficher des données structurées dans un tableau à deux colonnes. l'entete d'un tableau (un tablerow) est écrit en dur dans le xml, et les lignes de données sont construites dynamiquement au chargement de mon activity chaque ligne est composé de deux textview, ayant le même weight. code xml coresspondant au tableau: <TableLayout android:id="@+id/tableTravaux" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4083BD"> <TableRow> <TextView android:text="Travaux" android:textColor="#FFFFFF" android:gravity="center" android:background="#4083BD" android:layout_weight="1"/> <TextView android:text="Ressources" android:textColor="#FFFFFF" android:gravity="center" android:background="#4083BD" android:layout_weight="1"> </TextView> </TableRow> </TableLayout> ensuite dans l'activity je boucle sur une collection pour charger les lignes de données table = (TableLayout) findViewById(R.id.tableTravaux); for (Tache t : mestaches) { // create a new TableRow TableRow row = new TableRow(this); TextView tvTrav = new TextView(this); tvTrav.setText(t.getLibelle()); TextView tvRess = new TextView(this); String users = "TODO recup users"; tvRess.setText(users); row.addView(tvTrav); row.addView(tvRess); table.addView(row); } jusque la tout va bien, mes données s'affiche, c'est moche mais ca s'affiche si je veut ensuite applique un weight a mes textView pour que chaque colonne ai la même largeur, je rajoute ce bout de code table = (TableLayout) findViewById(R.id.tableTravaux); for (Tache t : mestaches) { // create a new TableRow TableRow row = new TableRow(this); TextView tvTrav = new TextView(this); tvTrav.setText(t.getLibelle()); TextView tvRess = new TextView(this); String users = "TODO recup users"; tvRess.setText(users); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.weight= 1.0f; tvTrav.setLayoutParams(params); tvRess.setLayoutParams(params); row.addView(tvTrav); row.addView(tvRess); table.addView(row); } et la c'est le drame, mes données ne s'affichent plus. si j'applique le layoutparams a un seul élément, seul l'autre s'affiche. a noter que j'ai tester toutes les combinaisons de paramètre entre fill_parent, match_parent, tout ca, sans succès. j'ai aussi essayer de placer ce bout de code apres le row.addview, la j'ai un classCastException j'ai aussi essayer d'ajouter le layoutparam directement en paramètre dans le row.addView, toujours pareil. j'ai aussi essayer de créer deux objets layoutParam, un par textView au cas ou, toujours pareil. Bref je suis un peu court d'idées, j'attend vos suggestions avec impatience merci d'avance! Link to comment Share on other sites More sharing options...
chpil Posted March 13, 2012 Share Posted March 13, 2012 Modifie ton layout comme ce qui suit: <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableTravaux" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4083BD" android:stretchColumns="0,1"> <TableRow> <TextView android:background="#4083BD" android:gravity="center" android:text="Travaux" android:textColor="#FFFFFF" /> <TextView android:background="#4083BD" android:gravity="center" android:text="Ressources" android:textColor="#FFFFFF" > </TextView> </TableRow> </TableLayout> (suppression des android:layout_gravity sur les TextView, ajout du android:strechColumns sur le TableLayout) et cela devrait fonctionner (sans utilisation de LayoutParams dans le code ensuite) Link to comment Share on other sites More sharing options...
bobby51 Posted March 14, 2012 Author Share Posted March 14, 2012 j'ai réussi a m'en sortir, au niveau du xml c'était bon, c'était le code java qui merdait, je n'utilisait pas les bon type de layoutparams pour le row, et en plus mettre le width a 0 pour ce layout param, et c'est ok. sujet résolu! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.