user3282877
user3282877

Reputation: 23

How to set both width and height of layouts as percentages in android?

I need to create a horizontal layout as follows :Layout Pic

I can set the total width of the layout of C using android:layout_width="match_parent" but I'm completely stumped at setting the rest of the layouts. Any help is highly appreciated. The whole app is set to run on a horizontal layout only, if that helps in decide what method to set the layouts.

Upvotes: 2

Views: 3718

Answers (2)

PieterAelse
PieterAelse

Reputation: 3678

The trick is to use LinearLayout's with a certain weight like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:weightSum="10">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">


        <View
            android:id="@+id/view_A"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:id="@+id/view_B"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <View
        android:id="@+id/view_C"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="9" />

</LinearLayout>

Upvotes: 0

Sonali8890
Sonali8890

Reputation: 2005

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:weightSum="100" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="90"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:background="#FF0000" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="50"
        android:background="#00FF00" >
    </LinearLayout>
 </LinearLayout>

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="10"
    android:background="#FFFF00" >
 </LinearLayout>

</LinearLayout>

Upvotes: 5

Related Questions