basickarl
basickarl

Reputation: 40474

Android XML: Percentage Height and width to create unique layout

I've heard so much about weight etc. However most of the tutorials refer always to width percentage. I wish to do the following setups with layouts: enter image description here

Anyone know how to do this? I'm stuck!

Upvotes: 2

Views: 4148

Answers (2)

reVerse
reVerse

Reputation: 35264

Edit :

Since the Percent Support Library now exists it's not necessary to use a LinearLayout with weights anymore.


Well ok, you're first layout should be pretty easy, check the following code:

<?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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:weightSum="3" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:background="@android:color/darker_gray" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:background="@android:color/white" >
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="9" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:background="@android:color/holo_red_light" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="6"
                android:background="@android:color/holo_orange_light" >
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:weightSum="9" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="6"
                android:background="@android:color/holo_blue_dark" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:background="@android:color/holo_green_dark" >
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Output:

Output

The second one seems not possible to do with weights. Furthere more I would recommend that you don't use too much nested Linear Layouts with weights since this is a huge performance issue according to the following Link

Upvotes: 3

Swapnil Kotwal
Swapnil Kotwal

Reputation: 5730

android:layout_weight=".YOURVALUE" is best way to implement in percentage

<?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:orientation="vertical" >
<TextView
    android:id="@+id/logTextBox"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight=".20"
    android:maxLines="500"
    android:scrollbars="vertical"
    android:singleLine="false"
    android:text="@string/logText" >
</TextView>

</LinearLayout>

Upvotes: 0

Related Questions