Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

Android equal sizing on linearlayouts

Okay so I would like the following layout:

enter image description here

What I've tried is to use linearlayouts with weights. I really dont want to put hardcoded widths on each element to make them right. Thing is that I just cant get that large box to be side by side with the 2nd box on row 1, and I dont seem to get why. Can I achieve this just in XML, using weights or something similar?

My layout so far:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal"
    android:weightSum="4" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1" >
    </TextView>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="2" >
    </TextView>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="3" >
    </TextView>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="4" >
    </TextView>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal"
    android:weightSum="4" >

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="5" />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_weight="3"
        android:text="Box x3 incl. margins" />

</LinearLayout>

Upvotes: 1

Views: 68

Answers (2)

user543
user543

Reputation: 3633

Try this...

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" >

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:layout_weight="1"
        android:text="button1" />

    <Button
        android:layout_weight="1"
        android:text="button2" />

    <Button
        android:layout_weight="1"
        android:text="button3" />

    <Button
        android:layout_weight="1"
        android:text="button4" />
</TableRow>

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button android:text="button5" />

    <Button
        android:layout_span="3"
        android:text="button6" />
</TableRow>

</TableLayout>

Upvotes: 1

piotrpo
piotrpo

Reputation: 12636

Probably this is possible to achieve vith LinearLayouts but... Using weights, nested LinearLayouts is expensive. Today I've worked on optimization of really complex layout that taken 14s to render on high level devices - after changes without changing look of it - less than 1s.

What you looking for is probably TableLayout. Here you have the solution: http://www.mkyong.com/android/android-tablelayout-example/

Upvotes: 1

Related Questions