好吧,我想要以下布局:



我尝试过的是使用带有权重的线性布局。我真的不想在每个元素上添加硬编码宽度以使其正确。问题是我只是无法让大盒子与第1行的第二个盒子并排而已,我似乎不明白为什么。我可以使用权重或类似方法仅通过XML来实现吗?

到目前为止我的布局:

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

最佳答案

可能这可以实现LinearLayouts,但是...
使用权重,嵌套的LinearLayouts非常昂贵。今天,我已经在真正复杂的布局上进行了优化,在不更改外观的情况下,只需14s即可在高级设备上进行渲染-少于1s。

您正在寻找的可能是TableLayout。这里您有解决方案:
http://www.mkyong.com/android/android-tablelayout-example/

08-04 02:16