2017-12-29 6 views
0

Ich bereite ein Tic-Tac-Spiel vor: Zunächst sind alle 9 Zellen leer. Beim Start Spiel, wie ich Nummer eingeben, ändert sich die Breite der Zelle dynamisch.Wie das zu verhindern?Wie make TextViews eine Zeilenbreite füllen?

width of Last column becomes equal spaced as i enter a number in third column

Meine xml codeof 9 Zellen:

<GridLayout 
    android:id="@+id/grid_layout_id" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginHorizontal="20dp" 
    android:background="@android:color/holo_red_dark" 
    android:rowCount="3" 
    android:columnCount="3" 
    android:padding="10dp" 
    android:theme="@style/MyTextViewStyle"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="1" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="2" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="4" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="5" /> 

    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="7" /> 

    <TextView 
     android:id="@+id/textView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="8" /> 

    <TextView 
     android:id="@+id/textView9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

</GridLayout> 

PS: Ich möchte nicht, Tabellen-Layout, Satz von Horizontale und vertikale Linear Layouts oder andere Ansichten verwenden. Bitte sagen Sie mir, wie Sie es in GridLayout selbst tun.

Antwort

1

In jedem TextView setzte die layout_width 0DP. Anfänglich wird TextView 0 Pixel groß sein, aber sie werden den verbleibenden Platz als Ergebnis von: android:layout_columnWeight="1" gleichermaßen füllen.

1

Siehe die Antwort here. Sie müssen nur das folgende Stück auf jeder Textview ändern:

android:layout_width="0dp" 
0

Sie müssen eine feste Gewichtung auf jede Spalte Ihrer Zeile setzen. Um dies zu tun, müssten Sie in jeder Komponente in diesem Fall Textview, die PES zuweisen, die sie in der Zeile haben werden. Wenn Ihre Zeile 2 Komponenten haben soll, können Sie den Wert der Summe des Gewichtes in weight_sum = "1" zuweisen und dann in jeder Komponente das Gewicht, das Sie haben, wenn Sie sie zum Beispiel gleich würden, würden Sie, layout_weight = 0.5 .So in diesem So hätten Sie jede Komponentenbreite innerhalb des Containers festgelegt.

Ich hoffe, es war Ihre Hilfe.

Ihr Code:

<GridLayout 
    android:id="@+id/grid_layout_id" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginHorizontal="20dp" 
    android:background="@android:color/holo_red_dark" 
    android:rowCount="3" 
    android:columnCount="3" 
    weight_sum="3" 
    android:padding="10dp" 
    android:theme="@style/MyTextViewStyle"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_weight="1" 
     android:layout_rowWeight="1" 
     tools:text="1" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_weight="1" 
     android:layout_rowWeight="1" 
     tools:text="2" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_weight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="4" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="5" /> 

    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="7" /> 

    <TextView 
     android:id="@+id/textView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="8" /> 

    <TextView 
     android:id="@+id/textView9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     tools:text="" /> 

</GridLayout> 
0

Ich kann sehen, dass Sie android:layout_columnWeight="1" verwendet haben, sondern auch android:layout_width="wrap_content" verwendet. Wie in diesem post erwähnt, ist es nicht notwendig Layout_width-Parameter in GridLayout zu verwenden und sollte daher vermieden werden.

Versuchen Sie Ihre Layout_width und layout_height auf einen festen Wert, sagen wir 10 dp, dann würde Android lieber Layout_columnWeight verwenden und gleichen Teil jeder Textansicht zuweisen. Lesen Sie auch post, um die Abwärtskompatibilität von layout_width zu ermöglichen.