Wie richte ich das lineare Layout an der vertikalen Mitte aus?

Lesezeit: 5 Minuten

Benutzer-Avatar
SJ Lim

Ich versuche, die vertikale Mitte von LinearLayout auszurichten, die das folgende Bild (Skycolor-Rand) zeigt, um die vertikale Mitte der Schaltfläche zu löschen.

Also setze ich die Schwerkraft von id:groupNumbers auf center_vertical.

aber keine geändert.

Wie richte ich id:groupNumbers auf die Mitte der Schaltfläche aus?

Geben Sie hier die Bildbeschreibung ein

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/groupNumbers"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_weight="0.7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/txtSelected01"
                    android:text="00"
                    android:textSize="30dp"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
        </LinearLayout>

        <LinearLayout
                android:orientation="horizontal"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/txtSelected02"
                    android:text="00"
                    android:textSize="30dp"
                    android:gravity="center_horizontal"
                    android:layout_weight="1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"/>
        </LinearLayout>
    </LinearLayout>


    <Button
            android:id="@+id/btn_deleteNum"
            android:text="Delete"
            android:textSize="20dp"
            android:layout_weight="0.2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

  • Schwerkraftattribut für lineares Layout prüfen.

    – Nikkasch

    25. November 2013 um 5:28 Uhr

  • Sich bewerben android:layout_gravity="center_vertical" zu deinem Layout…

    – Abhishek Patel

    25. November 2013 um 5:29 Uhr

  • Verwenden Sie ein relatives Layout, da dies diese Ausrichtungen zu einfach macht. Wir können dies auch mit LinearLayout erreichen, aber mit etwas Schmerz.

    – Rajesh Bath

    25. November 2013 um 6:47 Uhr


Benutzer-Avatar
MysticMagicϡ

Ändern Sie die Ausrichtung und die Schwerkraft ein

<LinearLayout
    android:id="@+id/groupNumbers"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_weight="0.7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

zu

android:orientation="vertical"
android:layout_gravity="center_vertical"

Sie fügen Ausrichtung hinzu: horizontal, sodass das Layout alle Elemente in einer einzigen horizontalen Linie enthält. Was es Ihnen nicht erlaubt, das Element in die Mitte zu bekommen.

Hoffe das hilft.

  • Danke an alle. Ich habe mir vorgenommen, dies zu tun. Ich habe die Schwerkraft des ersten Layouts auf Mitte gesetzt. schemas.android.com/apk/res/android

    ” android:orientation=”horizontal” android:gravity=”center” android:layout_width=”match_parent” android:layout_height=”wrap_content”>

    – SJ Lim

    25. November 2013 um 5:43 Uhr


  • aber das ist nicht, was er will … er will eine einzelne horizontale Linie in der Mitte des vertikalen Bildschirms …

    – Raffael Lima

    4. Mai 2018 um 22:29 Uhr

verwenden android:layout_gravity Anstatt von android:gravity

android:gravity legt die Schwerkraft des Inhalts der Ansicht fest, in der es verwendet wird.
android:layout_gravity legt die Schwerkraft der Ansicht oder des Layouts in ihrem übergeordneten Element fest.

  • Vielen Dank! Das war sehr hilfreich, obwohl die Antwort so alt ist.

    – Chris Kraszewski

    10. April 2018 um 11:22 Uhr

Benutzer-Avatar
Laurent Choy

Verwenden layout_gravity Anstatt von gravity. layout_gravity teilt dem Elternteil mit, wo es positioniert werden soll, und gravity sagt seinem Kind, wo es positioniert werden soll.

<LinearLayout
    android:id="@+id/groupNumbers"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:layout_weight="0.7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

Benutzer-Avatar
Mandy

Bei mir habe ich das Problem mit behoben android:layout_centerVertical="true" bei einem Elternteil RelativeLayout:

<RelativeLayout ... >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerVertical="true">

</RelativeLayout>

Benutzer-Avatar
Gen Bo

Für eine Box, die in der Mitte erscheint – horizontal und vertikal – habe ich dies mit nur einem LinearLayout zum Laufen gebracht. Die Antwort von Viswanath L war sehr hilfreich

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/layout_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/dialog_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="Error"
        android:textColor="#000" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="Error-Message"
        android:textColor="#000" />


    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/message_text"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Ok" />


</LinearLayout>

Benutzer-Avatar
Somwang Souksavatd

Verwenden Sie RelativeLayout innerhalb von LinearLayout

Beispiel:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Status"/>
        </RelativeLayout>
</LinearLayout>

Benutzer-Avatar
Udara Abeythilake

Verwenden android:weightSum -Eigenschaft an das übergeordnete LinearLayout und geben Sie den Wert 3 an. Dann in den untergeordneten LinearLayout-Verwendungen android:layout_weight 2 bzw. 1. Dann im First Chil LinearLayout verwenden android:layout_gravity="center_vertical". Wie im folgenden Code gezeigt

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="3"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:layout_gravity="center_vertical"
                        android:orientation="horizontal">
                        <TextView
                            android:id="@+id/status_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="00"
                            android:textColor="@color/red"
                            android:textSize="@dimen/default_text_size"
                            />
                          <TextView
                            android:id="@+id/status_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="00"
                            android:textColor="@color/red"
                            android:textSize="@dimen/default_text_size"
                            />
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1">
                        <Button
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:text="Delete"/>
                    </LinearLayout>
                </LinearLayout>

1284090cookie-checkWie richte ich das lineare Layout an der vertikalen Mitte aus?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy