Ich schreibe Code für eine Aktivität, die eine Tabelle hat, die zunächst 2 Zeilen enthält (siehe Screenshot). Ich möchte, dass der Text in diesen zwei Zeilen in der Mitte liegt (nicht in der Textansicht).android: Schwerkraft ändert nicht die Position des Textes in einem TextView
Die Einstellung android:gravity = "center"
oder android:gravity = "center_horizontal"
wirkt sich jedoch nicht auf den Text aus.
Was fehlt mir?
activity_game.xml:
<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="match_parent"
android:background="@android:color/white"
android:orientation="horizontal"
android:padding="16dp"
tools:context=".MainActivity">
<TableLayout
android:id="@+id/tl1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="@+id/tr_header_p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/player1_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 1"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/tl2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TableRow
android:id="@+id/tr_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:id="@+id/player2_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player 2"
android:textColor="#000000"
android:textSize="24sp" />
</TableRow>
<TableRow
android:id="@+id/tr_header2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/table_row_bg"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/table_cell_bg"
android:gravity="center"
android:text="Guess"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/table_cell_bg"
android:gravity="center"
android:text="Bulls"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cows"
android:textColor="#000000"
android:textSize="16sp" />
</TableRow>
</TableLayout>
Works. Können Sie erklären, warum ? Sollte dies nicht nur die Textansicht in die Mitte verschieben, aber da die layout_width der TextView match_parent ist, sollte es überhaupt keinen Effekt haben. –
Erklärung siehe meine Bearbeitung 1 –
Wenn meine Antwort Ihnen hilft, bitte als Antwort markieren und schließen die Frage –