image/svg+xml $ $ ing$ ing$ ces$ ces$ Res Res ea ea Res->ea ou ou Res->ou r r ea->r ch ch ea->ch r->ces$ r->ch ch->$ ch->ing$ T T T->ea ou->r

ViewGroup

Agencement des composants

Comment implanter un ViewGroup ?

La mesure

Conseils pour l'implantation de onMeasure(int widthMeasureSpec, int heightMeasureSpec) :

Comment interroger les composants enfants sur leurs dimensions souhaitées :

Le positionnement

Layout

Les dimensions

Possibilité de définir des dimensions pour les composants ajoutés sur un layout :

Layouts

L'API propose certains layouts permettant de créer un agencement de composants sous la forme de fichiers XML (ou programmatiquement).

Lorsque l'on place un composant graphique sur un layout, des paramètres de placements (LayoutParams) peuvent être utilisés (sous la forme d'attributs XML ou d'une instance de LayoutParams).

Paramètres d'agencement communs à tous les layouts :

Approches possibles pour mettre en oeuvre un agencement complexe :

FrameLayout

Exemple de FrameLayout :

LinearLayout vertical
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/testFrameLayout"
    android:layout_height="match_parent"
    tools:context=".layoutest.LayoutestActivity">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_gravity="center"
        android:src="@android:drawable/sym_action_call"/>

    <AnalogClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/analogClock"
        android:layout_gravity="left|top" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Answer the phone"
        android:id="@+id/button1"
        android:layout_gravity="right|bottom"
        android:paddingBottom="100dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:src="@android:drawable/arrow_down_float"
        android:minHeight="100dp"
        android:minWidth="100dp"
        android:layout_gravity="right|top" />

</FrameLayout>

LinearLayout

Exemple avec une orientation verticale :

LinearLayout vertical
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/testLinearLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button #0"
        android:id="@+id/button0" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Button #1"
        android:layout_gravity="left" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="Button #2"
        android:layout_gravity="right" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="Button #3"
        android:layout_gravity="center" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_weight="1"
        android:id="@+id/button4"
        android:text="Button #4"
        android:padding="30dp"
        android:layout_marginTop="100dp"
        android:layout_gravity="left" />
</LinearLayout>

RelativeLayout

Exemple de RelativeLayout :

RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<RelativeLayout 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:id="@+id/testRelativeLayout"
    tools:context=".layoutest.LayoutestActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio1"
        android:id="@+id/radio0"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:checked="false" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio2"
        android:id="@+id/radio1"
        android:layout_below="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:checked="false" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/toggleButton"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/radio1"
        android:layout_alignEnd="@+id/radio1"
        android:layout_alignLeft="@+id/radio1"
        android:layout_alignStart="@+id/radio1"
        android:checked="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text on a TextView!"
        android:id="@+id/textView3"
        android:layout_below="@+id/radio0"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@+id/textView3"
        android:layout_alignRight="@+id/textView3"
        android:layout_alignEnd="@+id/textView3"
        android:text="EditText" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_below="@+id/editText"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:layout_alignBottom="@+id/toggleButton"
        android:layout_toLeftOf="@+id/toggleButton"
        android:layout_toStartOf="@+id/toggleButton"
        android:src="#0f0ff1" />

</RelativeLayout>

TableLayout

Exemple de TableLayout :

TableLayout
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<TableLayout 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:id="@+id/testTableLayout"
    tools:context=".layoutest.LayoutestActivity">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/button7"
            android:layout_column="0" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:id="@+id/button8"
            android:layout_column="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:id="@+id/button9"
            android:layout_column="2"
            android:layout_weight="1" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/button4"
            android:layout_column="0"
            android:layout_weight="0" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:id="@+id/button5"
            android:layout_column="1"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:id="@+id/button6"
            android:layout_column="2"
            android:layout_weight="0" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/button1"
            android:layout_column="0"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/button2"
            android:layout_column="1"
            android:layout_weight="0" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:id="@+id/button3"
            android:layout_column="2"
            android:layout_weight="1" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/button0"
            android:layout_column="0"
            android:layout_span="3"
            android:layout_weight="1"
            android:paddingTop="100dp"
            android:focusableInTouchMode="false"
            android:textSize="50sp" />
    </TableRow>
</TableLayout>

GridLayout

GridLayout vertical
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/testGridLayout"
    android:layout_height="match_parent"
    android:columnCount="3"
    android:rowCount="5">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/button0"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_rowWeight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/button1"
        android:layout_row="1"
        android:layout_column="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/button2"
        android:layout_row="2"
        android:layout_column="2"
        android:layout_columnWeight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/button3"
        android:layout_row="3"
        android:layout_column="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        android:id="@+id/button4"
        android:layout_row="4"
        android:layout_column="0"
        android:layout_rowWeight="1"/>
</GridLayout>

ConstraintLayout

Contraintes de placement relatif

Positionnement relatif possible par rapport à un autre composant (en indiquant son id) ou par rapport au parent (désigné par parent).

Dimensions

Marges

Possibilité de définir des marges (comme pour la plupart des layouts) :

Si un composant disparaît en visibilité GONE, la marge du composant adjacent continue d'être appliquée par rapport au prochain composant visible. Ce comportement est modifiable en indiquant une marge spéciale si le composant adjacent est GONE :

Largeur et hauteur

Possibilité de définir comme pour tous les layouts les dimensions d'un composant enfant (layout_width et layout_height) en utilisant les valeurs suivantes :

Contraintes contradictoires

Exemple : définissons un TextView avec les contraintes suivantes :

Si le texte du TextView est plus petit que la largeur du parent, les trois conditions sont impossibles à satisfaire simultanément.
Solution proposée par ConstraintLayout :

Chaînes

Chains

Flow : des chaînes avec retour à la ligne

Barrières et guides

Groupes

Exemple avec un groupe référençant quatre boutons par leur id :

 <androidx.constraintlayout.widget.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button0,button1,button2,button3" />

Exemple de ConstraintLayout

ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<!-- Code sample from Coursand [http://igm.univ-mlv.fr/~chilowi/], under the Apache 2.0 License -->

<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="Horizontal bias=0.25, vertical weight=1"
        app:layout_constraintBottom_toTopOf="@id/textView2"
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintVertical_weight="1.0" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimaryDark"
        android:gravity="center"
        android:text="Horizontal bias=0.75, vertical weight=2"
        app:layout_constraintBottom_toTopOf="@id/textView3"
        app:layout_constraintHorizontal_bias="0.75"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        app:layout_constraintVertical_weight="2.0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:background="#0f50dd"
        android:gravity="center"
        android:text="Aspect ratio 16:9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="20dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Défilement

Comment rendre un layout défilable ?

Attention, certains composants graphiques intègrent déjà par défaut le défilement vertical (inutile des les insérer dans un ScrollView), il s'agit de :