Allow layout to occupy% of the screen?

Can a layout be allowed to occupy a percentage of screen space?

I have 2 layouts that currently occupy about 130dip height, and this works great for most screens, but when I switch to landscape, another linearlayout becomes barely noticeable at certain screen sizes. How would I insure that each of them be provided with an equal screen space?

+3
source share
3 answers

Set each layout to the same value for android:layout_weight.

Example:

<?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"
    >
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#FF0000" />
    <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" 
                  android:layout_weight="1" android:background="#0000FF" />
</LinearLayout>
+4
source

use android:layout_weight, here are the docs with exmaple :

Here is an example:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    >
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dip"
        android:layout_weight="0.5"
    />
</LinearLayout>

, LinearLayouts .

+1

set android:layout_weight = "1"in each of the elements you want to use equal space. This will allow you to work with any screen size.

the setting android:layout_widthwill be the same for all, will NOT work for all screen sizes.

... however usage layout_weightwill automatically distribute each equally regardless of screen size

0
source

All Articles