Möchte einen Bildschirm für meine App mit zwei LinearLayouts teilen. Welche Parameter sollte ich verwenden, um eine exakte Aufteilung in zwei gleiche Teile vorzunehmen – das erste LinearLayout oben und das zweite direkt darunter.
Wie teilt man den Bildschirm mit zwei gleichen LinearLayouts?
Konstantin Burow
Verwenden Sie die layout_weight
Attribut. Das Layout sieht ungefähr so aus:
<LinearLayout android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"/>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="0dp"/>
</LinearLayout>
-
Sehen Sie sich dieses Tutorial zur Verwendung des Attributs layout_weight an schach-ix.com/2012/01/17/…
– Valentin Despa
24. September 2012 um 18:19 Uhr
-
sollte das Gewicht nicht 0,5 und 0,5 sein?
– Marek
5. Juli 2013 um 2:21 Uhr
-
@Marek Soweit ich mich erinnere, spielt es keine Rolle, solange sie gleich sind.
– Konstantin Burov
7. Juli 2013 um 7:48 Uhr
Silber
Ich beantworte diese Frage nach 4-5 Jahren, aber Best Practices, um dies wie folgt zu tun
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/secondView"
android:orientation="vertical"></LinearLayout>
<View
android:id="@+id/secondView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/secondView"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Dies ist der richtige Ansatz als Verwendung von Layout_Gewicht ist immer schwer für UI-Operationen. Das gleichmäßige Aufteilen des Layouts mit LinearLayout ist keine gute Praxis
-
Wirklich tolle Antwort 🙂
– Arlind
6. Juni 2015 um 11:37 Uhr
-
@silwar was ist, wenn ich horizontal gleiche lineare Layouts haben möchte?
– Woppi
8. Februar 2017 um 7:04 Uhr
-
@Woppi – überprüfen Sie diesen Kern und lassen Sie mich wissen, ob dies Ihre Frage beantwortet. gist.github.com/silwar/ba2679baf5d5681796228e2fe9d01645
– Silber
8. Februar 2017 um 10:55 Uhr
Einfach mal rausstellen:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:weightSum="4"
android:padding="5dp"> <!-- to show what the parent is -->
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
Um die Benutzeroberfläche in zwei gleiche Teile aufzuteilen, können Sie verwenden GewichtSumme von 2 im Elternteil LinearLayout und zuordnen Layout_Gewicht von 1 zu jedem wie unten gezeigt
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Um ein Layout in gleiche Teile zu teilen
Verwenden Sie Layoutgewichtungen. Denken Sie daran, dass die Einstellung wichtig ist layout_width
wie 0dp
auf Kinder, damit es wie beabsichtigt funktioniert.
auf übergeordnetem Layout:
- Satz
weightSum
des übergeordneten Layouts als1
(android:weightSum="1"
)
auf dem untergeordneten Layout:
- Satz
layout_width
wie0dp
(android:layout_width="0dp"
) - Satz
layout_weight
wie0.5
[half of weight sum fr equal two] (android:layout_weight="0.5"
)
Aufteilen des Layouts in drei gleiche Teile:
- Elternteil:
weightSum
3 - Kind:
layout_weight
: 1
Layout in vier gleiche Teile aufteilen:
- Elternteil:
weightSum
1 - Kind:
layout_weight
: 0,25
Layout in n gleiche Teile aufteilen:
- Elternteil:
weightSum
n- Kind:
layout_weight
: 1
Unten sehen Sie ein Beispiellayout zum Aufteilen des Layouts in zwei gleiche Teile.
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView .. />
<EditText .../>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView ../>
<EditText ../>
</LinearLayout>
</LinearLayout>
Fredmanzi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_marginTop="16dp"
android:textSize="18sp"
android:textStyle="bold"
android:padding="4dp"
android:textColor="#EA80FC"
android:fontFamily="sans-serif-medium"
android:text="@string/team_a"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/team_a_score"
android:text="@string/_0"
android:textSize="56sp"
android:padding="4dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/team_a_fouls"
android:text="@string/fouls"
android:padding="4dp"
android:textSize="26sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/_1_points"
android:layout_width="match_parent"
android:onClick="addOnePointTeamA"
android:textColor="#fff"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="@string/_2_points"
android:textColor="#fff"
android:onClick="addTwoPointTeamA"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="@string/_3_points"
android:textColor="#fff"
android:onClick="addThreePointTeamA"
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/_1_point_foul"
android:textColor="#fff"
android:layout_width="match_parent"
android:onClick="addOnePointFoulTeamA"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="@string/team_b"
android:textColor="#EA80FC"
android:textStyle="bold"
android:padding="4dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-medium"
android:textSize="18sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/team_b_score"
android:text="0"
android:padding="4dp"
android:textSize="56sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/team_b_fouls"
android:text="Fouls"
android:padding="4dp"
android:textSize="26sp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/_1_points"
android:textColor="#fff"
android:fontFamily="sans-serif-medium"
android:layout_width="match_parent"
android:onClick="addOnePointTeamB"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
<Button
android:text="@string/_2_points"
android:layout_margin="6dp"
android:fontFamily="sans-serif-medium"
android:textColor="#fff"
android:onClick="addTwoPointTeamB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/_3_points"
android:fontFamily="sans-serif-medium"
android:textColor="#fff"
android:onClick="addThreePointTeamB"
android:layout_margin="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/_1_point_foul"
android:textColor="#fff"
android:onClick="addOnePointFoulTeamB"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<Button
android:text="@string/reset"
android:layout_marginBottom="25dp"
android:onClick="resetScore"
android:textColor="#fff"
android:fontFamily="sans-serif-medium"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Codemacher
Verwenden android:layout_weight
und android:weightSum
Layouts in gleiche Teile zu teilen. Es ist wichtig einzustellen layout_width
wie 0dp
auf Kinder, damit es wie beabsichtigt funktioniert.
Auf übergeordnetem Layout:
Satz weightSum
des übergeordneten Layouts als 2
(android:weightSum="2"
)
Auf dem untergeordneten Layout:
Satz layout_width
wie 0dp
(android:layout_width="0dp"
) Satz layout_weight
wie 1
[half of weight sum] (android:layout_weight="1"
)
<!-- Parent layout -->
<LinearLayout
android:id="@+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<!-- Child layout -->
<LinearLayout
android:id="@+id/layout_child_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
...
</LinearLayout>
<LinearLayout
android:id="@+id/layout_child_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
...
</LinearLayout>
</LinearLayout>
Verwenden Sie für jedes Layout eine Gewichtung von 0,5
– Sephi
6. August 2010 um 15:10 Uhr
Die Gewichte beider Layouts sollten “gleich” sein, müssen kein Bruch sein
– Siddharth
5. Juli 2013 um 3:24 Uhr