1

我正在构建一个非常复杂的布局,它代表以下结构:

所需布局

它具有以下布局 XML:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button01"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="L" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M" />

        <Button
            android:id="@+id/Button03"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="R" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="--------------------" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="100"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button05"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="L" />

        <Button
            android:id="@+id/Button02"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M" />

        <Button
            android:id="@+id/Button04"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="R" />
    </LinearLayout>
</LinearLayout>

现在我需要像这样嵌套这块:

嵌套布局

该结构在过去更加复杂,由于调用堆栈大小为 8/16 KB,导致第 3 个嵌套级别的堆栈溢出。当前布局可以处理 12 个嵌套级别而不会溢出,但测量时间非常高(ANR 也是),因为我使用的是嵌套权重:

以毫秒为单位测量时间:从父母到孩子

  • 2000 1950 970 1200 500 500 240 245 135 120 70 70 35 45 21 15 8 8 4 3 1 1 0.3 0.01

    • 有没有办法进一步扁平化层次结构?
    • 有没有办法摆脱嵌套的权重?
    • 在单独的线程中测量并同时显示一个等待圈-> 如何?
    • 从单独的线程调用 parent.addView 并在一个线程中完成整个工作以防止堆栈溢出......这将出现在 12 个嵌套级别之外的某个地方。如何实施?这里已经提到过:Android:增加调用堆栈大小
4

2 回答 2

0

您可以将顶级 LinearLayout 替换为 RelativeLayout。

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

    <LinearLayout
        android:id="@+id/topRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button01"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="L" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M" />

        <Button
            android:id="@+id/Button03"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="R" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_below="@+id/topRow"
        android:layout_alignLeft="@+id/topRow"
        android:layout_alignRight="@+id/topRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="--------------------" />

    <LinearLayout
        android:id="@+id/bottomRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/Button05"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="L" />

        <Button
            android:id="@+id/Button02"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="M" />

        <Button
            android:id="@+id/Button04"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.1"
            android:text="R" />
    </LinearLayout>
</RelativeLayout>
于 2014-05-05T18:26:01.620 回答
0

在 android 中,你的布局越扁平,你的应用程序就越流畅和快速,“扁平”是指尽可能少地使用嵌套布局。对于您发布的第二张照片(嵌套布局),我建议使用 html/css ,因为实施这样的布局肯定会对性能造成巨大压力,即使它运行成功。如果您想实现游戏,请尝试针对此类繁重布局的其他解决方法。

于 2014-05-07T06:20:46.060 回答