Usted tiene que preocuparse acerca weightSum
atributo de la disposición lineal. Luego, los atributos layout_weight
, layout_height
y layout_width
de las vistas interiores. Es todo lo que necesita para dividir el diseño en cualquier proporción.
Si desea dividir la disposición en dos partes de igual tamaño que tienen que establecer layout_width
atributos a 0dp
de ambos diseños y layout_weight
a 1
dentro del diseño principal. A continuación, establezca weightSum
en 2
del diseño principal.
El mismo concepto se puede aplicar para dividir el diseño en cualquier proporción. Establezca todos los valores dentro de layout_weight
suma hasta weightSum
. A continuación, configure layout_width
o layout_height
en 0dp
de acuerdo con la orientación que desea dividir. Aquí está el diseño de la solución para esta pregunta.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainActivity"
android:weightSum="2">
<fragment
android:id="@+id/mainFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
aquí hay un ejemplo para dividir LinearLayout a 1, 3, 2 relación verticalmente.
<?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="vertical"
android:weightSum="6">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@android:color/black">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/white">
</RelativeLayout>
</LinearLayout>