Ich habe oft versucht, ein relatives Layout zu zeichnen, das horizontal ausgerichtet und in einen halben Bildschirm unterteilt ist.
Ich entwerfe das Bild mit Paint, um etwas besser zu erklären, was ich meine.
Irgendein Vorschlag?
Sie können diese 2 RelativeLayouts in horizontaler Ausrichtung in ein LinearLayout einfügen und dann weight für beide RelativeLayouts verwenden. Dadurch wird das LinearLayout in zwei gleiche Teile aufgeteilt.
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="horizontal"
Android:baselineAligned="false">
<RelativeLayout
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
Eine andere Möglichkeit, um dieselbe Aufgabe zu erledigen, ohne ein LinearLayout verwenden zu müssen, besteht darin, ein zentriertes "Shim" in die Mitte des übergeordneten Layouts zu setzen und dann andere Elemente daran auszurichten. Wenn Sie die Breite des halben Breitenelements auf "match_parent" festlegen, aber sowohl die linke als auch die rechte Seite ausrichten, werden sie am Ende verkleinert, damit sie passen.
<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"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.EqualWidthExample" >
<!-- An invisible view aligned to the center of the parent. Allows other
views to be arranged on either side -->
<View
Android:id="@+id/centerShim"
Android:layout_height="match_parent"
Android:layout_width="0dp"
Android:visibility="invisible"
Android:layout_centerHorizontal="true"/>
<!--Set width to match_parent sets maximum width. alignParentLeft aligns
the left Edge of this view with the left Edge of its parent. toLeftOf
sets the right Edge of this view to align with the left Edge of the
given view. The result of all three settings is that this view will
always take up exactly half of the width of its parent, however wide
that may be. -->
<Button
Android:id="@+id/btLeft"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_alignParentLeft="true"
Android:layout_toLeftOf="@+id/centerShim"
Android:text="Left Button" />
<!--Same deal, but on the right -->
<Button
Android:id="@+id/btRight"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_alignParentRight="true"
Android:layout_toRightOf="@+id/centerShim"
Android:layout_below="@+id/tvLeft"
Android:text="Right Button" />
</RelativeLayout>
mit PercentRelativeLayout ist das jetzt ganz einfach
<Android.support.percent.PercentRelativeLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:padding="16dp"
tools:context=".MainActivity">
<Button
Android:id="@+id/button"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:text="Button"
app:layout_widthPercent="50%"/>
<Button
Android:id="@+id/button2"
Android:layout_height="wrap_content"
Android:layout_centerVertical="true"
Android:layout_toRightOf="@id/button"
Android:text="Button 2"
app:layout_widthPercent="50%"/>
</Android.support.percent.PercentRelativeLayout>
vergiss nicht, die Gradle-Abhängigkeit hinzuzufügen
dependencies {
compile 'com.Android.support:percent:25.3.1'
}
Update
Ich sehe in Ihrer Zeichnung 4 relativelayouts ...?
Wenn Sie meinen, dass die beiden in der Mitte in ein LinearLayout (horizontale Ausrichtung) gesetzt werden, lassen Sie alle eine Breite von match_parent haben und geben Sie beiden relativen Layouts eine Gewichtung von 1