简体   繁体   中英

Two columns with one column split into two rows - Android

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

在此处输入图片说明

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>

You can achieve this via a combination of LinearLayout s 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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM