Android – TextView horizontal in LinearLayout zentrieren

Lesezeit: 3 Minuten

Benutzer-Avatar
Jake Wilson

Ich habe folgendes Grundlayout

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

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bar_background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="10dp"
        android:text="HELLO WORLD" />

    </LinearLayout>
<LinearLayout>

Es scheint, als ob die XML-Datei korrekt ist, aber der Text linksbündig ausgerichtet ist. Die Textansicht nimmt die gesamte Breite des Elternteils ein und die Textansicht ist zentriert. Nicht sicher, was das Problem ist …

  • versuchen Sie android:gravity=”center” für Ihre Textansicht

    – Benutzer370305

    4. Oktober 2011 um 17:47 Uhr


Benutzer-Avatar
Dan S

Was passiert ist, dass seit dem die TextView die füllt ganz Breite des inneren LinearLayouts befindet es sich bereits in der horizontalen Mitte des Layout. Wenn Sie verwenden android:layout_gravity es platziert das Widget als Ganzes in der angegebenen Schwerkraft. Anstatt das gesamte Widget-Center zu platzieren, versuchen Sie wirklich, das zu platzieren Inhalt in der Mitte, die mit erreicht werden kann android:gravity="center_horizontal" und die android:layout_gravity Attribut kann entfernt werden.

  • Wenn ich das richtig verstehe, könnte er “android:layout_width=”fill_parent” in “wrap_content” ändern und dann android:layout_gravity=”center_horizontal” verwenden.Habe ich Recht?

    – Paul Brewczyński

    4. Dezember 2013 um 20:59 Uhr

  • @bluesm Nein, das innere LinearLayout lässt keinen Platz zu, der nicht mit einer Ansicht gefüllt ist (ohne Berücksichtigung des Falls eines leeren LinearLayout). So die android:layout_width denselben Wert haben (nach dem Layout). Da die Breite der TextView gleich der Breite des inneren LinearLayout ist, hat die TextView effektiv die android:layout_gravity Werte von left, rightund center zur selben Zeit.

    – Dan S

    4. Dezember 2013 um 22:20 Uhr


  • Zum ImageView es ist layout_gravityund für TextView es ist gravity ist, was nur funktioniert. Android ist großartig! Danke an alle schönen SO-Posts, ohne die Entwicklung einfach unmöglich war.

    – Atul

    6. August 2016 um 12:36 Uhr


  • nicht vergessen android:layout_width="match_parent"

    – Choletski

    3. November 2016 um 15:03 Uhr


Benutzer-Avatar
patel135

Wenn Sie einstellen <TextView> in der Mitte <Linearlayout> dann zuerst setzen android:layout_width="fill_parent" verpflichtend
Keine Notwendigkeit, eine andere Schwerkraft zu verwenden

    <LinearLayout
            android:layout_toRightOf="@+id/linear_profile" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal">
            <TextView 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="It's.hhhhhhhh...."
                android:textColor="@color/Black"

                />
    </LinearLayout>

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

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar_background">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:padding="10dp"
    android:text="HELLO WORLD" />

</LinearLayout>

Benutzer-Avatar
Schwarzer Gürtel

Verwenden android:gravity="center" in TextView Anstatt von layout_gravity.

Benutz einfach: android:layout_centerHorizontal=”true”

Es wird die gesamte Textansicht in die Mitte stellen

1328480cookie-checkAndroid – TextView horizontal in LinearLayout zentrieren

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

Privacy policy