简体   繁体   中英

Android equal sizing on linearlayouts

Okay so I would like the following layout:

在此处输入图片说明

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>

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/

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>

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