Comment centrer horizontalement un bouton dans un RelativeLayout?


Alex

Après avoir d'abord cherché une solution (par exemple ici ), je suis tombé sur la solution suivante:

android:layout_centerHorizontal="true"

ce qui ne fonctionne pas pour moi (voir troisième bouton). Vous trouverez ci-dessous l'exemple de code complet de trois boutons dans a RelativeLayout, dans lequel le bouton du milieu doit être centré (horizontalement et verticalement, ce que le bouton est), et les deux autres boutons sont censés être symétriques placés sur l'écran, centrés horizontalement. Cependant, ils ne le sont pas, en utilisant la solution suggérée que j'ai trouvée plusieurs fois.

Alors qu'est-ce que je manque?

Code complet:

<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:id="@+id/checkscreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.impyiablue.checkpoint.CheckScreen">

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        android:orientation="vertical">


        <Button
            android:id="@+id/check_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/check_next"
            android:textSize="20sp"
            android:padding="25dip"
            android:layout_alignParentTop="true"
            android:layout_alignLeft="@+id/check_now"
            android:layout_alignStart="@+id/check_now"
            android:layout_marginTop="50dp" />

        <Button
            android:id="@+id/check_now"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/check_now"
            android:padding="70dip"
            android:textSize="20sp" />

        <Button
            android:id="@+id/check_redo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/check_redo"
            android:textSize="20sp"
            android:padding="25dip"
            android:layout_alignParentBottom="true"
            android:layout_alignLeft="@+id/check_now"
            android:layout_alignStart="@+id/check_now"
            android:layout_marginBottom="50dp" />

    </RelativeLayout>


</LinearLayout>

Capture d'écran

paradis

»

<Button
            android:id="@+id/check_redo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/check_redo"
            android:textSize="20sp"
            android:padding="25dip"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="50dp" />

`vous pouvez essayer d'utiliser RelativeLayout comme mise en page racine, cela rendra les choses plus faciles à gérer.

Articles connexes


Comment centrer un bouton dans un div?

Foreyez J'ai un div dont la largeur est de 100%. J'aimerais centrer un bouton dedans, comment puis-je faire cela? <div style="width:100%; height:100%; border: 1px solid"> <button type="button">hello</button> </div> Loktar Réponse mise à jour Mise à jour parc

Centrer horizontalement un bouton MDL

Andrew Existe-t-il un moyen simple de centrer horizontalement un mdl-button? Un bouton comme celui-ci: <button class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect"> <i class="material-icons">add</i> </button> centré uniquement horizontaleme

Comment centrer <input> horizontalement dans un <div> ?

LegusX J'ai essayé plusieurs fois, avec plusieurs solutions possibles différentes autour de Stack Overflow pour accomplir cette tâche, mais de manière frustrante, je n'arrive pas à centrer la zone de saisie dans le div. Mon code ressemble essentiellement à cec

Comment centrer horizontalement un bouton absolu dans un div ?

Haytam J'essaie de centrer un bouton absolu dans un div (en bas), mais cela semble ne pas fonctionner. Voici ce que je fais pour l'instant : .mc-item { background: #F0F0F0; border: 1px solid #DDD; height: 140px; padding: 20px; } .mc-item a { positio

Comment centrer un Positioned() horizontalement dans Flutter

Tomique Je construis actuellement un écran de code de vérification pour mon application et j'utilise une pile avec des widgets positionnés (), car le formulaire de code de vérification doit également être bien positionné, mais comment puis-je le centrer horizo