2010-08-06 7 views
52

Vous voulez partager un écran pour mon application avec deux LinearLayouts. Quels paramètres devrais-je utiliser pour faire une division exacte en deux parties égales - d'abord LinearLayout en haut et le second est juste en dessous.Comment diviser l'écran avec deux LinearLayouts égaux?

+0

utilisez le poids = 0,5 pour chaque mise en page – Sephy

+2

les poids des deux mises en page doivent être "identiques", pas nécessairement une fraction – Siddharth

Répondre

105

Utilisez le paramètre de poids, à peu près la mise en page ressemblera à ceci:

<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

Vous avez orthographié le troisième 'LinearLayout' erroné. – Doomsknight

+0

@Doomsknight thx, fixe! –

+2

Jetez un oeil à ce tutoriel concernant l'utilisation de l'attribut layout_weight http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/ –

11

juste le mettre là-bas:

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

Je réponds à cette question au bout de 4-5 ans, mais les meilleures pratiques pour faire cela comme ci-dessous

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

cette bonne approche est que l'utilisation de layout_ poids est toujours lourd pour les opérations d'interface utilisateur. Diviser la mise en page de façon égale à l'aide de LinearLayout n'est pas une bonne pratique

+0

Vraiment super réponse :) – Arlind

+0

fonctionne comme un charme. bon état de l'art répondre – MPhil

+0

Ce sera la bonne réponse. –

Questions connexes