views:

370

answers:

1

I am writing an android application that has 3 views one below the other. Each view has to be of equal height, depending on the screen size. Each view occupies the full width of the screen. How do you specify in the layout xml that the view has to take up 1/3rd height of the device display height? What would be the appropriate layout to use in this case?

+7  A: 

Try android:layout_weight="1"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View  
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"
    android:background="#F00"
    />
<View  
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"
    android:background="#0F0"
    />
<View  
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"
    android:background="#00F"
    />

bhatt4982
How does this specify the height to be equally distributed?
Ram
From the android docs: http://developer.android.com/guide/topics/ui/layout-objects.html Look for the weight explanation under the LinearLayout section.
Scott
Specifying android:layout_weight="1" for each child view will direct layout to distribute height equally. Please refer to docs pointed by Scott.
bhatt4982