Android-Layout rechts ausrichten

Lesezeit: 6 Minuten

Benutzer-Avatar
Señor Reginold Francis

Ich habe folgendes Layout an Ort und Stelle

<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_weight="1">


        <WebView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webview" android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:layout_weight="13">
        <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
                    <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="https://stackoverflow.com/questions/4305564/@drawable/back" android:padding="10dip" />
            </LinearLayout>

            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
                <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
            </LinearLayout>

        </LinearLayout>

        <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent"    android:layout_weight="1" >
                <ImageButton android:background="@null" android:id="@+id/special"   android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
        </RelativeLayout>




    </LinearLayout>


</LinearLayout>

Für den Zweck dieser Frage geht es mir nur um die untere Hälfte des Layouts. Im Moment enthält es 3 Bildschaltflächen. Die ersten 2 möchte ich rechts nebeneinander linksbündig ausrichten. Der dritte, ich möchte auf der rechten Seite ausgerichtet werden.

So wie sie sind, sind die ersten beiden Schaltflächen dort, wo ich sie haben möchte, aber die dritte bleibt hartnäckig linksbündig. Wie würde ich es richtig ausgerichtet machen.

Das Layout ist extrem ineffizient und aufgebläht. So viele braucht man nicht LinearLayouts. Eigentlich braucht man keine LinearLayout überhaupt.

Verwenden Sie nur einen RelativeLayout. So was.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageButton android:background="@null"
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="https://stackoverflow.com/questions/4305564/@drawable/back"
        android:padding="10dip"
        android:layout_alignParentLeft="true"/>
    <ImageButton android:background="@null"
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/forward"
        android:padding="10dip"
        android:layout_toRightOf="@id/back"/>
    <ImageButton android:background="@null"
        android:id="@+id/special"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/barcode"
        android:padding="10dip"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

  • Ich glaube, die Antwort auf die gestellte Frage lautet: android:layout_alignParentRight=”true” Es ist ein wenig schwer in Ihrer Antwort zu sehen.

    – eriig

    2. Juni um 17:57 Uhr


Sie können das alles mit nur einem tun RelativeLayout (was übrigens nicht nötig ist android:orientation Parameter). Anstatt also eine LinearLayoutdie eine Menge Zeug enthält, können Sie Folgendes tun:

<RelativeLayout>
    <ImageButton
        android:layout_width="wrap_content"
        android:id="@+id/the_first_one"
        android:layout_alignParentLeft="true"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/the_first_one"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

Wie Sie bemerkt haben, fehlen einige XML-Parameter. Ich habe nur die grundlegenden Parameter gezeigt, die Sie eingeben mussten. Den Rest kannst du vervollständigen.

Wenn Sie verwenden möchten LinearLayoutkönnen Sie die Ausrichtung mit vornehmen layout_weight mit Space Element.

ZB folgende Anlageorte textView und textView2 nebeneinander u textView3 wird rechtsbündig ausgerichtet

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2" />

    <Space
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3" />
</LinearLayout>

Sie können den gleichen Effekt auch ohne erzielen Space wenn du setzen würdest layout_weight zu textView2. Es ist nur so, dass ich die Dinge getrennter mag, und es zu demonstrieren Space Element.

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2" />

Beachten Sie, dass Sie sollten (müssen aber nicht) einstellen layout_width ausdrücklich, da es sowieso nach seinem Gewicht neu berechnet wird (genauso sollten Sie die Höhe in Elementen von vertical LinearLayout). Weitere Tipps zur Layoutleistung finden Sie unter Android-Layout-Tricks Serie.

Dies ist ein Beispiel für a RelativesLayout:

RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                relativeLayout.setLayoutParams(params);

Bei einer anderen Art von Layout (zB LinearLayout) muss man einfach umsteigen RelativesLayout zum LinearLayout.

Um ältere Versionen zu unterstützen, kann Space wie unten durch View ersetzt werden. Fügen Sie diese Ansicht zwischen der Komponente ganz links und der Komponente ganz rechts hinzu. Diese Ansicht mit Gewicht = 1 wird gedehnt und den Raum ausfüllen

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

Hier finden Sie einen vollständigen Beispielcode. Es hat 4 Komponenten. Zwei Pfeile befinden sich auf der rechten und linken Seite. Der Text und der Spinner befinden sich in der Mitte.

    <ImageButton
        android:id="@+id/btnGenesis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="2dp"
        android:background="@null"
        android:gravity="left"
        android:src="https://stackoverflow.com/questions/4305564/@drawable/prev" />

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lblVerseHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/spinnerVerses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="center"
        android:textSize="25sp" />

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btnExodus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="2dp"
        android:background="@null"
        android:gravity="right"
        android:src="@drawable/next" />
</LinearLayout>

1251930cookie-checkAndroid-Layout rechts ausrichten

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

Privacy policy