2011-05-11 75 views
6

que tienen la siguiente distribución:Android: ¿Cómo dividir LinearLayout en dos partes exactamente del mismo tamaño?

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:orientation="horizontal" 
    android:weightSum="2"> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="ch.lexs.view.LawList" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    </fragment> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:name="ch.lexs.view.ParagraphList" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 
    </fragment> 

</LinearLayout> 

Mi intención era dividir el LinearLayout en dos partes que son exactamente del mismo tamaño. Pensé que esto se puede hacer usando peso. Pero el segundo fragmento es mucho más pequeño. ¿Qué estoy haciendo mal?

Respuesta

7

Intente establecer los anchos de ambos en "fill_parent".

2

Tiene el layout_height de ambos fragmentos establecidos en "fill_parent" ¿Tal vez intente sin eso?

0

Intenta configurar android: layout_height = "0" si quieres que los diseños tengan la misma altura, o android: layout_width = "0" si quieres el mismo ancho.

8

este código tal vez ayudará a

<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" > 

    <fragment 
     android:id="@+id/mainFragment" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     class="com.lmhuan.weatherapp.helper.MainFragment" > 
    </fragment> 

    <fragment 
     android:id="@+id/detailFragment" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     class="com.lmhuan.weatherapp.helper.DetailFragment" > 
    </fragment> 

</LinearLayout> 
0

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> 
0

fill_parent está en desuso por lo instaed de ello se puede utilizar match_parent

Cuestiones relacionadas