Programmatisch TableRow zu TableLayout hinzufügen

X

xdroid-man

Neues Mitglied
0
Hallo,

ich versuche einem im XML definiertem TabellenLayout programmatisch Tabellenzeilen (TableRow) hinzuzufügen. Den Tabellenzeilen möchte ich pro Zeile 3 Views hinzufügen, auch wieder programmatisch.
Die einzelnen Views sollen Bilder enthalten. Da die Bilder recht Groß sind wollte ich die View größentechnisch mit LayoutParams begrenzen aber ich scheitere immer. Wenn ich die Zeile



[FONT=&quot]image.setLayoutParams(new LayoutParams(75, 75));
[/FONT]

[FONT=&quot]
[/FONT]

einkommentiere sehe ich gar nichts. Lass ich die Zeile weg ist das Bild viel zu groß.


Code:
  [COLOR=#0000C0][FONT=&quot]tableRow[/FONT][/COLOR][COLOR=black][FONT=&quot] = [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]TableRow[/FONT][/COLOR][COLOR=black][FONT=&quot]([/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]this[/FONT][/COLOR][/B][COLOR=black][FONT=&quot]);[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]tableRow[/FONT][/COLOR][COLOR=black][FONT=&quot].setLayoutParams([/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]LayoutParams[/FONT][/COLOR][COLOR=black][FONT=&quot]([/FONT][/COLOR][COLOR=#9B009B][FONT=&quot]LayoutParams[/FONT][/COLOR][COLOR=black][FONT=&quot].[U]FILL_PARENT[/U], 75));[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=&quot]for[/FONT][/COLOR][/B][COLOR=black][FONT=&quot]( [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]int[/FONT][/COLOR][/B][COLOR=black][FONT=&quot] x = 0; x < pathesToImages.size() ; x++ )[/FONT][/COLOR]
  [COLOR=black][FONT=&quot]    {[/FONT][/COLOR]
  [COLOR=#9B009B][FONT=&quot]Log[/FONT][/COLOR][COLOR=black][FONT=&quot].[I]d[/I]([/FONT][/COLOR][COLOR=#2A00FF][FONT=&quot]"Lars Koslowski"[/FONT][/COLOR][COLOR=black][FONT=&quot],x + [/FONT][/COLOR][COLOR=#2A00FF][FONT=&quot]": "[/FONT][/COLOR][COLOR=black][FONT=&quot] + pathesToImages.get(x).toString());[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]image[/FONT][/COLOR][COLOR=black][FONT=&quot] = [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]ImageView[/FONT][/COLOR][COLOR=black][FONT=&quot]([/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]this[/FONT][/COLOR][/B][COLOR=black][FONT=&quot]);[/FONT][/COLOR]
  [COLOR=#3F7F5F][FONT=&quot]//image.setLayoutParams(new LayoutParams(75, 75));[/FONT][/COLOR]
  [COLOR=#9B009B][FONT=&quot]File[/FONT][/COLOR][COLOR=black][FONT=&quot] foto = [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]File[/FONT][/COLOR][COLOR=black][FONT=&quot](pathesToImages.get(x).toString());[/FONT][/COLOR]
  [COLOR=#9B009B][FONT=&quot]BitmapFactory[/FONT][/COLOR][COLOR=black][FONT=&quot].[/FONT][/COLOR][COLOR=#9B009B][FONT=&quot]Options[/FONT][/COLOR][COLOR=black][FONT=&quot] options = [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]
[/FONT][/COLOR]
[COLOR=#9B009B][FONT=&quot]BitmapFactory[/FONT][/COLOR][COLOR=black][FONT=&quot].[/FONT][/COLOR][COLOR=#9B009B][FONT=&quot]Options[/FONT][/COLOR][COLOR=black][FONT=&quot]();[/FONT][/COLOR]
  [COLOR=#3F7F5F][FONT=&quot]//options.inSampleSize = 2;[/FONT][/COLOR]
  [COLOR=#9B009B][FONT=&quot]Bitmap[/FONT][/COLOR][COLOR=black][FONT=&quot] bitmap = [/FONT][/COLOR][COLOR=#9B009B][FONT=&quot]BitmapFactory[/FONT][/COLOR][COLOR=black][FONT=&quot].[I]decodeFile[/I](foto.getAbsolutePath(), options);[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]image[/FONT][/COLOR][COLOR=black][FONT=&quot].setImageBitmap(bitmap);[/FONT][/COLOR][COLOR=#3F7F5F][FONT=&quot]//setImageURI(Uri.fromFile([U]foto[/U]));[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]image[/FONT][/COLOR][COLOR=black][FONT=&quot].setPadding(2, 2, 2, 2);         [/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]tableRow[/FONT][/COLOR][COLOR=black][FONT=&quot].addView([/FONT][/COLOR][COLOR=#0000C0][FONT=&quot]image[/FONT][/COLOR][COLOR=black][FONT=&quot]);[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=&quot]if[/FONT][/COLOR][/B][COLOR=black][FONT=&quot] (x == 2)[/FONT][/COLOR]
  [COLOR=black][FONT=&quot]                  {[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]table[/FONT][/COLOR][COLOR=black][FONT=&quot].addView([/FONT][/COLOR][COLOR=#0000C0][FONT=&quot]tableRow[/FONT][/COLOR][COLOR=black][FONT=&quot]);[/FONT][/COLOR]
  [COLOR=#0000C0][FONT=&quot]tableRow[/FONT][/COLOR][COLOR=black][FONT=&quot] = [/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]new[/FONT][/COLOR][/B][COLOR=#9B009B][FONT=&quot]TableRow[/FONT][/COLOR][COLOR=black][FONT=&quot]([/FONT][/COLOR][B][COLOR=#7F0055][FONT=&quot]this[/FONT][/COLOR][/B][COLOR=black][FONT=&quot]);[/FONT][/COLOR]
  [COLOR=black][FONT=&quot]                  }[/FONT][/COLOR]
  [COLOR=black][FONT=&quot]            }[/FONT][/COLOR]
Ich würde mich über Hilfe freuen.


Grüße
 
Hi, ich empfehle dir ein Layout zu inflaten. android - How to inflate one view with an layout - Stack Overflow

Dann kannst du deine LayoutParameter noch im XML-Definieren und auch in der Vorschau anschauen und dann brauchst du nur noch die Bilder auszutauschen.
Wäre meiner Meinung auch Best Practice. (Bei mir gibt es sogar in einem Kalender mit über 30 "geinflateten" Relativelayouts zur Zeit keine Performanceprobleme.
 

Ähnliche Themen

S
Antworten
0
Aufrufe
589
Sergio13
S
E
Antworten
2
Aufrufe
775
ekaya999
E
A
Antworten
1
Aufrufe
583
swa00
swa00
Zurück
Oben Unten