1
votes

I'm trying to implement a layout that separates the screen 40/60 vertically, using layout_weight.

Here is the xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/white" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" />
    </LinearLayout>
</LinearLayout>

This code does not work, returning a blank screen. However, if I change

android:layout_width="fill_parent"

android:layout_height="0dp"

android:layout_weight="0.4"

(Vertical split)

to

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="0.4"

(Horizontal split)

the code works as intended and the layout splits 40/60 horizontally. Why is it not working vertically, and how can I fix this? Any help would be appreciated!

1
give parent layout orientation to verticalAnmol317
@Anmol Strange, could've sworn I tried that and it didn't work... Thank you though! Also, do you mind answering the question instead of replying so that I can mark this post as answered?Ryalis
Sure and welcomeAnmol317

1 Answers

2
votes

give parent layout orientation to vertical

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:focusable="true"
android:orientation="verticle"
android:focusableInTouchMode="true"
android:background="@drawable/background">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.4">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/white" />
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.6">
    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black" />
</LinearLayout>