2016-07-06 5 views
0

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?

Screenshot

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> 

Antwort

1

dies umgehen, android:gravity="center" zu Ihrem @+id/tr_header_p1 und @+id/tr_header wie

<TableRow 
    android:id="@+id/tr_header_p1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:padding="5dp"> 

und

hinzufügen Versuchen Sie sollten

<TableRow 
    android:id="@+id/tr_header" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:padding="5dp"> 

das könnte Ihnen helfen

EDIT 1

oder benötigen Sie den android:layout_weight="1" zu Ihrem Textview zu erwähnen. wie

<TextView 
     android:id="@+id/player2_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1" 
     android:text="Player 2" 
     android:textColor="#000000" 
     android:textSize="24sp" /> 
+0

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. –

+0

Erklärung siehe meine Bearbeitung 1 –

+0

Wenn meine Antwort Ihnen hilft, bitte als Antwort markieren und schließen die Frage –

0

Versuch für die TableRow Eltern von Textview

android:gravity="CENTER_HORIZONTAL" 

das Standardlayout einer Tabelle, um Kinder Knoten angewendet wird, wie in der Dokumentation beschrieben here

+1

Hat nicht funktioniert, nichts geändert –

+0

@SajalNarang änderte ich die Antwort, gelten 'center_horizontal' auf die Elternteil dieser Textansichten – ddb

+0

Das Hinzufügen des Attributs android: gravity zum Elternteil macht den Trick, aber ich verstehe nicht warum.Soweit ich weiß, versucht es, TextView auf die Mitte von TableRow auszurichten, aber scheitert, weil die layout_width der TextView auf match_parent gesetzt ist. Warum hat es Auswirkungen auf den Inhalt des TextView? –

0

Try this:

<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:gravity="center" 
      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:gravity="center" 
      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> 
</LinearLayout> 
0

Sie können versuchen, layout_width-wrap_content Einstellung und stellen Sie dann layout_gravity-center.

, dass die gesamte Textview und nicht der Text im Inneren

0

ausrichten Was passiert, wenn Sie android verwendet: layout_gravity von Ihrem Textview, die im Begriff, die Position des Elements seine Eltern nach beeinflussen wird? Es gibt einen Thread, um die Differenz zwischen den beiden unter

erklären

Gravity and layout_gravity on Android

+0

Der Android: layout_width des TextView ist auf Match_parent gesetzt, also Android: layout_gravity sollte keine Auswirkungen auf die Positio haben n, oder? –

+0

Sie haben Recht! Wenn du zu ** wrap_content ** wechselst, solltest du gut sein. Aber es liegt an Ihnen und Ihrem Projektumfang. –

0
<TableRow 
     android:id="@+id/tr_header" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/table_row_bg" 
     android:gravity="center_horizontal" 
     android:padding="5dp"> 

     <TextView 
      android:id="@+id/player2_name" 
      android:layout_width="match_parent" 
      android:gravity="center" 
      android:text="Player 2" 
      android:textColor="#000000" 
      android:textSize="24sp" /> 
    </TableRow> 

Dies funktioniert für mich

Verwandte Themen