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?
Répondre
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>
Vous avez orthographié le troisième 'LinearLayout' erroné. – Doomsknight
@Doomsknight thx, fixe! –
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/ –
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>
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
utilisez le poids = 0,5 pour chaque mise en page – Sephy
les poids des deux mises en page doivent être "identiques", pas nécessairement une fraction – Siddharth