Two columns with one column split into two rows - Android

2.3k views Asked by At

I am trying to implement a TableLayout that has two columns and the second column is split into two rows. Something like this:

enter image description here

What i have so far is something like this which is basically creating three columns:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:id="@+id/tr1"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        >

        <ImageView
            android:src="@drawable/icon"
            android:layout_span="1"
            android:layout_height="160dp"
            android:layout_width="200dp"
            android:scaleType="centerCrop"
            />

        <ImageView
            android:src="@drawable/icon"
            android:layout_height="75dp"
            android:layout_width="wrap_content"
            android:scaleType="centerCrop"
            />

        <ImageView
            android:src="@drawable/icon"
            android:layout_height="75dp"
            android:scaleType="centerCrop"
            android:layout_width="wrap_content"/>

    </TableRow>

</TableLayout>
2

There are 2 answers

0
Hadi Satrio On

You can achieve this via a combination of LinearLayouts instead of TableLayout:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="144dp"
    android:layout_weight="0.5"
    android:background="@android:color/holo_red_dark">

    // Column A.
    // Put your content here..

</LinearLayout>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:background="@android:color/holo_green_dark"
        android:layout_weight="0.5">

        // Column B, row 1.
        // Put your content here..

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:background="@android:color/holo_blue_dark"
        android:layout_weight="0.5">

        // Column B, row 2.
        // Put your content here..

    </LinearLayout>

</LinearLayout>

Here's the layout it'll produce:

enter image description here

0
Kingfisher Phuoc On

You can use LinearLayout to achieve the same result:

<?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="100dp"
android:orientation="horizontal">

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="50"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="50"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="50"
        android:layout_height="0dp"
        android:orientation="vertical"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="50"
        android:orientation="vertical"
        android:layout_height="0dp"></LinearLayout>
</LinearLayout>
</LinearLayout>

However you can use RelativeLayout for better performance.