Ryalis
Ryalis

Reputation: 147

Android (Xamarin) linearlayout layout:weight not working as intended

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!

Upvotes: 1

Views: 2160

Answers (1)

Anmol317
Anmol317

Reputation: 1376

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>

Upvotes: 2

Related Questions