2010-08-06 40 views
52

Quiero dividir una pantalla para mi aplicación con dos LinearLayouts. ¿Qué parámetros debo usar para hacer una división exacta en dos partes iguales, primero LinearLayout en la parte superior y la segunda justo debajo de la misma.¿Cómo dividir la pantalla con dos LinearLayouts iguales?

+0

peso uso = 0,5 para cada diseño – Sephy

+2

los pesos de ambos diseños deben ser "igual", no necesita ser una fracción – Siddharth

Respuesta

105

Uso de parámetros de peso, aproximadamente diseño se verá así:

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

Usted al escribir la tercera 'LinearLayout' mal. – Doomsknight

+0

@Doomsknight thx, corregido! –

+2

Eche un vistazo a este tutorial sobre el uso del atributo layout_weight http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/ –

11

Sólo ponerlo por ahí:

<?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> 
35

estoy respondiendo a esta pregunta después de 4-5 años, pero las mejores prácticas para hacer esto como continuación

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

este enfoque correcto es como el uso de layout_ peso siempre es pesado para las operaciones de IU. Layout Splitting igualmente usando LinearLayout no es una buena práctica

+0

Muy buena respuesta :) – Arlind

+0

funciona como un encanto. buena respuesta de vanguardia – MPhil

+0

Esta será la respuesta correcta. –

Cuestiones relacionadas