GRIDLAYOUT

Ähnlich wie TabletLayout

Vorab werden aber die Spalten und Zeilen definiert

Die Zelle jedes UI-Elementes wird über column und row definiert. Ähnlich dem GridBagLayout.

Mit dem Attribut „gravity“ bestimmt man die Platzierung innerhalb der Zelle. Ähnlich dem GridBagLayout.

Dazu gibt es Konstanten

Das Zusammenfassen von Zellen geht mit:

columnSpan

rowSpan




Definition

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:rowCount="7"
    android:orientation="vertical"
    tools:context=".GridXMLActivity" >

    <Button
     android:id="@+id/button1"
     android:text="Ok"
     android:layout_width="wrap_content"				
     android:layout_height="wrap_content"
     android:layout_column="0"
     android:layout_row="0"
     android:layout_gravity="fill_horizontal"
    />

</GridLayout>



Beispiel

Komplettes Projekt
GridLayout1.7z





Ergebnis des 1. Beispiels

Bemerkung

Bemerkungen zum Beispiel
Der Button „Button8“ hat ein RowSpan.
Wenn aber keine weitere Reihe danach kommt,
geht dieser Schalter dann bis zum Ende des Views.

Abhilfe:
Man trägt eine zusätzliche Reihe ein.
Diese braucht kein Element haben !

Button8:
android:layout_gravity="center_vertical"



Beispielcode

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:rowCount="7"
    android:orientation="vertical"
    tools:context=".GridXMLActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_gravity="fill_horizontal"
            android:text="Button1">
        <Button
            android:id="@+id/button2"
            android:layout_column="0"
            android:layout_row="1"
            android:layout_gravity="fill_horizontal"
            android:text="Button2" />

        <Button
            android:id="@+id/button3"
            android:layout_column="1"
            android:layout_row="0"
            android:layout_gravity="fill_horizontal"
            android:text="Button3" />
        <Button
            android:id="@+id/button4"
            android:layout_column="1"
            android:layout_row="1"
            android:layout_gravity="fill_horizontal"
            android:text="Button4" />

        <Button
            android:id="@+id/button5"
            android:layout_column="2"
            android:layout_row="0"
            android:layout_gravity="fill_horizontal"
            android:text="Button5" />
        <Button
            android:id="@+id/button6"
            android:layout_column="2"
            android:layout_row="1"
            android:layout_gravity="fill_horizontal"
            android:text="Button5" />

        <Button
            android:id="@+id/button7"
            android:layout_column="0"
            android:layout_row="2"
            android:layout_columnSpan="2"
            android:layout_gravity="fill_horizontal"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:text="Button6" />

    <Button
        android:id="@+id/button8"
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnSpan="3"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:text="Button7" />

        <Button
            android:layout_column="0"
            android:layout_row="4"
            android:layout_gravity="center_vertical"
            android:layout_rowSpan="2"
            android:text="Button8" />

        <Button
            android:layout_column="1"
            android:layout_row="4"
            android:layout_gravity="fill_horizontal"
            android:text="Button9" />

        <Button
            android:layout_column="2"
            android:layout_row="4"
            android:layout_gravity="fill_horizontal"
            android:text="Button10" />


        <Button
            android:layout_column="1"
            android:layout_row="5"
            android:layout_gravity="fill_horizontal"
            android:text="Button11" />

        <Button
            android:layout_column="2"
            android:layout_row="5"
            android:layout_gravity="fill_horizontal"
            android:text="Button12" />



</GridLayout>






Konstante Wert Beschreibung
top 0x30 Objekt oben verankert, Größe bleibt.
bottom 0x50 Objekt unten verankert, Größe bleibt.
left 0x03 Objekt links verankert, Größe bleibt.
right 0x05 Objekt rechts verankert, Größe bleibt.
center_vertical 0x10 Objekt vertikal zentriert, Größe bleibt.
fill_vertical 0x70 Objekt wird komplett gestreckt.
center_horizontal 0x01 Objekt horizontal zentriert, Größe bleibt.
fill_horizontal 0x07 Objekt wird komplett gestreckt.
center 0x11 Objekt vertikal und horizintal zentriert, Größe bleibt.
fill 0x77 Objekt wird komplett gestreckt.
clip_vertical 0x80 Zusätzliche Option. Die Ecken verschmelzen mit dem Parent.
clip_horizontal 0x08 Zusätzliche Option. Die Ecken verschmelzen mit dem Parent.
start 0x00800003 Setzt Objekt ganz oben hin, Größe bleibt.
end 0x00800005 Setzt Objekt ganz unten hin, Größe bleibt.



Fragmente
Gravity-Konstanten