Android-Layout: Breite der Hälfte des übergeordneten Elements

Lesezeit: 4 Minuten

Ich habe ein sehr einfaches Layout, das ich nicht so aussehen lassen kann, wie ich es möchte. Es ist ein LinearLayout mit einer Schaltfläche und einem Schalter. Ich möchte, dass sie übereinander angezeigt werden, aber ich möchte, dass ihre Breite der Hälfte des übergeordneten Layouts entspricht.

|--LinearLayout---------|
|                       |
| -----------           |
||   Switch  |          |
| -----------           |
| -----------           |
||   button  |          |
| -----------           |
 ------------------------

Ich habe mir andere ähnliche Antworten in SO angesehen, aber ich konnte keine Lösung finden, die für mich funktioniert. Das habe ich bisher versucht:

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="2" >

                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/remember" />

                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />

            </LinearLayout>

Damit nehmen der Knopf und der Schalter den gesamten Platz des Elternteils ein, anstatt nur die Hälfte. Ich habe es mit versucht android:layout_width = 0dp bei den Kindern, aber es lässt sie verschwinden.

Irgendwelche Hilfe, bitte?

  • layout_weight wird nur auf das mit angegebene Maß angewendet orientation. Das heißt, die Gewichtung bezieht sich auf die Höhe in vertikaler und die Breite in horizontalen linearen Layouts.

    – laalto

    15. November 2013 um 12:08 Uhr

Benutzer-Avatar
ja

Eine Möglichkeit besteht darin, ein horizontales Master-LinearLayout zu haben, das die Breite auf 2 aufteilt und darin das vertikale Layout enthält

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <Switch
                android:id="@+id/remember_me_switch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/remember" />

            <Button
                android:id="@+id/share_button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:onClick="loginOnclick"
                android:text="@string/login_button_text" />

        </LinearLayout>
        <!-- Right side spacer -->
        <View
           android:layout_width="0dp"
           android:layout_height="1dp"
           android:layout_weight="1" />

    </LinearLayout>

  • Danke, das funktioniert für mich. Die Spacer-Ansicht hat es geschafft. Ich werde die Antwort so schnell wie möglich annehmen.

    – mario595

    15. November 2013 um 12:09 Uhr

Der Trick ist zu verwenden 0dp für die Größe, die Sie verwalten möchten Layout_Gewicht! Versuche dies

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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="2" >

            <Switch
                android:id="@+id/remember_me_switch"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:hint="@string/remember" />

            <Button
                android:id="@+id/share_button"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:onClick="loginOnclick"
                android:text="@string/login_button_text" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1" >
        </LinearLayout>

    </LinearLayout>

Kurze Antwort:

  1. Verwenden android:weightSum=”2″ in Ihrem horizontalen übergeordneten Layout
  2. Verwenden android:layout_weight=”1″ und android:layout_width=”0dp” in Ihren Kinderlayouts.

  • Sollte jedoch die akzeptierte Antwort sein, würde eine nette Erklärung es besser machen.

    – IOviSpot

    17. September 2021 um 14:15 Uhr

  • Sieht aus wie die exakt gleiche Lösung wie die bereits akzeptierte Antwort, nur ohne den Code und ~ 7,5 Jahre zu spät. Übersehe ich etwas?

    – nick_j_white

    16. April um 15:05 Uhr


  • das hat bei mir 2022 funktioniert!

    – Android-Entwickler

    31. Mai um 21:42 Uhr

Versuchen Sie diesen Code

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="2" >

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

                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/remember" />

               <View 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
            </LinearLayout>

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

                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />

               <View 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
             </LinearLayout>
        </LinearLayout>

Benutzer-Avatar
Kalai.G

Button button = (Button) findViewById(R.id.share_button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;

setze dies buttonWidth zu Ihrem Button wie button.setWidth(buttonWidth);

Benutzer-Avatar
Giulio Caccin

int params = linearLayout.getWidth()/2;

ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));

1054470cookie-checkAndroid-Layout: Breite der Hälfte des übergeordneten Elements

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

Privacy policy