Problem Ausrichtung innerhalb Tabellenzelle

  • Antworten:12
NoWo
  • Forum-Beiträge: 17

03.09.2012, 16:55:08 via Website

Hallo zusammen,

ich habe folgendes Problem:
Eine Tabelle enthält 1 Reihe, welche wiederum 4 Zellen enthält.
Der Inhalt der Zellen soll wie folgt ausgerichtet werden:
Zelle 1: unten
Zelle 2: egal
Zelle 3: unten
Zelle 4: mittig

Mein Code sieht folgendermaßen aus:
1package com.example.de.test.test;
2
3import android.app.Activity;
4import android.graphics.Color;
5import android.os.Bundle;
6import android.view.Gravity;
7import android.view.ViewGroup.LayoutParams;
8import android.widget.LinearLayout;
9import android.widget.TableLayout;
10import android.widget.TableRow;
11import android.widget.TextView;
12public class MainActivity extends Activity {
13
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17
18 LinearLayout LinearLayout = new LinearLayout(this);
19
20 TableLayout Tabelle1 = new TableLayout(this);
21 Tabelle1.setColumnStretchable(1, true);
22
23 TableRow tr = new TableRow(this);
24 tr.setBackgroundColor(Color.RED);
25
26 TextView td = new TextView(this);
27 td.setText("Zelle1");
28 td.setTextSize(15);
29 td.setBackgroundColor(Color.GREEN);
30 td.setGravity(Gravity.BOTTOM);
31 tr.addView(td);
32
33 td = new TextView(this);
34 td.setText("Zelle2");
35 td.setTextSize(30);
36 td.setBackgroundColor(Color.GREEN);
37 tr.addView(td);
38
39 td = new TextView(this);
40 td.setText("Zelle3");
41 td.setTextSize(15);
42 td.setBackgroundColor(Color.GREEN);
43 td.setGravity(Gravity.BOTTOM);
44 tr.addView(td);
45
46 td = new TextView(this);
47 td.setText("Zeile1\nZeile2");
48 td.setTextSize(15);
49 td.setBackgroundColor(Color.GREEN);
50 td.setGravity(Gravity.CENTER);
51 tr.addView(td);
52
53 Tabelle1.addView(tr);
54
55 TableLayout.LayoutParams Tabelle1_LayoutParams = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
56 LinearLayout.addView(Tabelle1,Tabelle1_LayoutParams);
57
58 TableLayout.LayoutParams LinearLayout_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
59 setContentView(LinearLayout,LinearLayout_LayoutParams);
60 }
61}

Leider kommt folgendes heraus:

Antworten
Florian B.
  • Forum-Beiträge: 284

03.09.2012, 18:16:51 via Website

Du setzt das falsche Gravity-Attribut. Du musst das layout_gravity Attribut setzen. Und auch die TextViews brauchen LayoutParameter für width und height. Via Code geht das wie folgt.

1TableLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
2params.gravity=Gravity.CENTER;
3textView.setLayoutParams(params);

Hat es einen bestimmten Grund warum das alles im Code und nicht im XML machst?

— geändert am 03.09.2012, 18:19:19

Antworten
NoWo
  • Forum-Beiträge: 17

03.09.2012, 18:29:34 via Website

Hallo Florian,

vielen Dank für deine Hilfe.
Leider scheint diese nicht so ganz zu funktionieren, da dadurch die komplette TextView verschwindet:



Ich mach das nur aus Gewohnheit im Code anstatt in der XML.

Hier mein neuer Code (es wurde das falsche Gravity auskommentiert und nur die letzte TextView mit deinem Vorschlag angepasst).
Ich kann leider nicht den ganzen Code posten, da zu identisch mit dem vorherigen Post.

1td = new TextView(this);
2 td.setText("Zeile1\nZeile2");
3 td.setTextSize(15);
4 td.setBackgroundColor(Color.GREEN);
5 // td.setGravity(Gravity.CENTER);
6
7 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
8 params.gravity=Gravity.CENTER;
9 td.setLayoutParams(params);
10
11 tr.addView(td);
12
13 Tabelle1.addView(tr);
14
15 TableLayout.LayoutParams Tabelle1_LayoutParams = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
16 LinearLayout.addView(Tabelle1,Tabelle1_LayoutParams);
17
18 TableLayout.LayoutParams LinearLayout_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
19 setContentView(LinearLayout,LinearLayout_LayoutParams);
20 }
21}

Antworten
Andreas Weichert
  • Forum-Beiträge: 287

03.09.2012, 18:54:32 via Website

Ich vermute Du mußt für die kleinen Textviews LayoutHeight = fill_parent setzen.
Es sieht so aus als wrap_content ob automatisch gesetzt ist.
Die Zelle ist so hoch wie ihr Inhalt (grüne Fläche), daher hat die Gravity keine Wirkung.

Nur ne Vermutung hab noch nicht mit Tabellen gearbeitet - aber bei anderen Layout ist das auch so.

Antworten
Florian B.
  • Forum-Beiträge: 284

03.09.2012, 21:59:44 via Website

Mh wenn ich näher darüber nachdenke, und nach Andreas Kommentar, müsste man das ganze eigentlich mit beiden gravitiy Attributen erreichen können.

layout_gravity bestimmt wie sich das Element zu seinem Eltern-Element verhält. Also in deinem Fall wie sich die TextView zur Tabellen-Zelle verhält. Mit dem normalen gravity legt man fest wie sich der Text in der TextView verhält.

Wenn du das ganze mit gravity machen willst, dann musst du, wie Andreas gesagt hat, die Höhe der TextViews auf fill_parent setzen.

1<?xml version="1.0" encoding="utf-8"?>
2<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TableRow android:layout_height="wrap_content"
8 android:layout_width="fill_parent"
9 android:background="#ff0000"
10 >
11
12 <TextView
13 android:layout_width="fill_parent"
14 android:layout_height="fill_parent"
15 android:background="#00ff00"
16 android:gravity="bottom"
17 android:text="Zelle 1"
18 android:textSize="15sp" />
19
20 <TextView
21 android:layout_width="fill_parent"
22 android:layout_height="fill_parent"
23 android:text="Zelle 2"
24 android:textSize="30sp"
25 android:background="#00ff00"
26 />
27
28 <TextView
29 android:layout_width="fill_parent"
30 android:layout_height="fill_parent"
31 android:text="Zelle 3"
32 android:textSize="15sp"
33 android:background="#00ff00"
34 android:gravity="bottom"
35 />
36
37 <TextView
38 android:layout_width="fill_parent"
39 android:layout_height="fill_parent"
40 android:text="Zelle 4"
41 android:textSize="15sp"
42 android:background="#00ff00"
43 android:gravity="center"
44 />
45 </TableRow>
46</TableLayout>

Antworten
NoWo
  • Forum-Beiträge: 17

04.09.2012, 09:20:36 via Website

Vielen Dank euch beiden, aber leider funktioniert das nicht so wie beabsichtigt, da die TextView/Zelle komplett verschwindet, sobald
android:layout_width="fill_parent"
android:layout_height="fill_parent"
gesetzt sind :(

Vielleicht könnt ihr mal ein neues Projekt anlegen und den Code ausführen, wie er bei euch rauskommt, vielleicht hat ja meine Entwicklungsumgebung nen Fehler..

Hier der Code:

1package com.example.de.test.test;
2
3import android.app.Activity;
4import android.graphics.Color;
5import android.os.Bundle;
6import android.view.Gravity;
7import android.view.ViewGroup.LayoutParams;
8import android.widget.LinearLayout;
9import android.widget.TableLayout;
10import android.widget.TableRow;
11import android.widget.TextView;
12public class MainActivity extends Activity {
13
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17
18 LinearLayout LinearLayout = new LinearLayout(this);
19
20 TableLayout Tabelle1 = new TableLayout(this);
21 Tabelle1.setColumnStretchable(1, true);
22
23 TableRow tr = new TableRow(this);
24 tr.setBackgroundColor(Color.RED);
25
26 TextView td = new TextView(this);
27 td.setText("Zelle1");
28 td.setTextSize(15);
29 td.setBackgroundColor(Color.GREEN);
30 TableLayout.LayoutParams td_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
31 td_LayoutParams.gravity=Gravity.BOTTOM;
32 tr.addView(td,td_LayoutParams);
33
34 td = new TextView(this);
35 td.setText("Zelle2");
36 td.setTextSize(30);
37 td.setBackgroundColor(Color.GREEN);
38 td_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
39 tr.addView(td,td_LayoutParams);
40
41 td = new TextView(this);
42 td.setText("Zelle3");
43 td.setTextSize(15);
44 td.setBackgroundColor(Color.GREEN);
45 td_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
46 td_LayoutParams.gravity=Gravity.BOTTOM;
47 tr.addView(td,td_LayoutParams);
48
49 td = new TextView(this);
50 td.setText("Zeile1\nZeile2");
51 td.setTextSize(15);
52 td.setBackgroundColor(Color.GREEN);
53
54 td_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
55 td_LayoutParams.gravity=Gravity.CENTER;
56 tr.addView(td,td_LayoutParams);
57
58 TableLayout.LayoutParams tr_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
59 Tabelle1.addView(tr,tr_LayoutParams);
60
61 TableLayout.LayoutParams Tabelle1_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT);
62 LinearLayout.addView(Tabelle1,Tabelle1_LayoutParams);
63
64 TableLayout.LayoutParams LinearLayout_LayoutParams = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
65 setContentView(LinearLayout,LinearLayout_LayoutParams);
66 }
67}

Antworten
Florian B.
  • Forum-Beiträge: 284

04.09.2012, 10:21:56 via Website

Wenn du das Layout das ich als XML gepostet habe, bei dir als Layout-Datei anlegst, passt es dann? Wenn wir wissen, dass das passt, dann müssen wir nur noch schauen, wie du es so im Code aufbauen kannst.

Antworten
NoWo
  • Forum-Beiträge: 17

04.09.2012, 10:46:48 via Website

Ja, die Layout-Datei passt, wird korrekt angezeigt.
Wäre nur die Frage, was im Code falsch ist, da dieser ja eigtl. genau das, was in der Layout-Datei steht, generieren sollte?

Antworten
Florian B.
  • Forum-Beiträge: 284

04.09.2012, 11:52:33 via Website

Ok, dann passt es als XML zumindest schon mal.

Also ganz stimmt dein Code mit dem XML nicht überein.

  1. Du hast als äußerstes Layout ein LinearLayout, das solltest du für den Anfang mal rauswerfen.
  2. Das würde ich auch mal vorerst rausnehmen:
    1Tabelle1.setColumnStretchable(1, true);

Antworten
NoWo
  • Forum-Beiträge: 17

04.09.2012, 12:10:48 via Website

Ich habe jetzt den Code entsprechend angepasst, das Problem ist unverändert: die TextViews (Zellen) werden nicht dargestellt und da es keine dargestellten TextViews gibt, gibt es wohl auch keine TableRow und dementsprechend wohl auch keine TableView. Es bleibt also eine weiße Seite.
Wenn ich jedoch für eine TextView ohne layout_width/layout_height (LayoutParams) generieren lasse, dann ist diese TextView sichtbar inkl. der roten Tabellenreihe.

Es scheint also evtl. ein Problem mit "new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)" zu geben bzw wird dadurch irgendetwas anderes beeinflusst..

Antworten
Florian B.
  • Forum-Beiträge: 284

04.09.2012, 13:35:55 via Website

Also ich würde ja fast dazu tendieren einfach das alles per XML zu machen. Sollte auch zum Entwickeln einfacher sein, da du Änderungen sofort im GUI Editor sehen kannst.

Antworten
NoWo
  • Forum-Beiträge: 17

04.09.2012, 13:45:18 via Website

Ja ich denke dass daran wohl kein Weg vorbeiführen wird.
Dennoch hätte ich mir erwünscht, dass dies per Code machbar gewesen wäre, nachdem ja auch die passenden Funktionen angeboten werden.
Wäre für mich die einfachere Lösung, aber was nicht geht geht halt nicht ;)

Auch würde ich gerne wissen, ob es sich hierbei um ein beabsichtigtes Verhalten handelt, oder ob Google hier nachbessern sollte ;D

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

04.09.2012, 14:11:45 via Website

Ich bin nicht unbedingt der Layout-Spezialist aber eines habe ich mir in all den Android Jahren angeeignet - vermeide TableLayouts.

Diese zu bändigen ist meist mehr Aufwand als z.B. gewichtete Elemente in einem LinearLayout. TableLayout ist eigentlich dafür designed dem Entwickler alles Denken abzunehmen. Die Ausrichtung, Formatierung, ... nimmt das System an Hand des Content automatisch selbst vor.

Ich hatte auch die <table>, <th>, <td>, etc. im Kopf als ich mein erstes TableLayout begann - und zack bin ich die selbe Falle gelaufen wie Du.

Antworten