Android Grid in xml erstellen
In Android
kann man ein Grid erstellen, indem man ein GridLayout als Element einbettet.
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="7"
android:background="@color/teal_200"
>
<Space
android:layout_column="0"
android:layout_row="0"
android:layout_margin="11dp"
/>
<Space
android:layout_column="1"
android:layout_row="0"
android:layout_margin="11dp"
/>
|
Mit RowCount
und ColumnCount kann man die vordefiniert Anzahl an Spalten und Zeilen
festlegen
android:columnCount="3"
android:rowCount="7"
|
Den
Abstand zwischen den Zeilen und Spalten kann man durch Space-Elemente erzielen
<Space
android:layout_column="0"
android:layout_row="0"
android:layout_margin="11dp"
/>
<Space
android:layout_column="1"
android:layout_row="0"
android:layout_margin="11dp"
/>
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="7"
android:background="@color/teal_200"
>
<Space
android:layout_column="0"
android:layout_row="0"
android:layout_margin="11dp"
/>
<Space
android:layout_column="1"
android:layout_row="0"
android:layout_margin="11dp"
/>
<Space
android:layout_column="2"
android:layout_row="0"
android:layout_margin="11dp"
/>
<Space
android:layout_column="0"
android:layout_row="1"
android:layout_margin="11dp"
/>
<Space
android:layout_column="0"
android:layout_row="1"
android:layout_margin="11dp"
/>
<TextView android:text="A1"
android:layout_column="0"
android:layout_row="0"
/>
<TextView android:text="B1"
android:layout_column="1"
android:layout_row="0"
/>
<TextView android:text="C1"
android:layout_column="2"
android:layout_row="0"
/>
<TextView android:text="A2"
android:layout_column="0"
android:layout_row="1"
/>
<TextView android:text="B2"
android:layout_column="1"
android:layout_row="1"
/>
<TextView android:text="C2"
android:layout_column="2"
android:layout_row="1"
/>
</GridLayout>
</LinearLayout>
|