【问题标题】:A more efficient layout possible?更有效的布局可能吗?
【发布时间】:2016-06-14 07:47:03
【问题描述】:

我的工作布局看起来像这样:

这是相应的xml:

<FrameLayout
    android:id="@+id/red"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        ...

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/green"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:background="@drawable/blueyellow_border">

                <ImageButton
                    android:id="@+id/blue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:background="@drawable/selector_blue_border"
                    android:padding="5dp"
                    android:src="@drawable/blue_icon"
                    tools:ignore="ContentDescription"/>

                <ImageButton
                    android:id="@+id/yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:layout_toRightOf="@id/blue"
                    android:background="@drawable/selector_yellow_border"
                    android:padding="5dp"
                    android:src="@drawable/selector_yellow_icon"
                    tools:ignore="ContentDescription"/>
            </RelativeLayout>

            <TextView
                android:id="@+id/green"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:background="@drawable/green_border"
                android:text="@string/green_string"/>

        </RelativeLayout>
</FrameLayout>

问题是根据层次视图,这种布局效率不高。下面我指的是 Hierarchy View 的红绿灯配色方案。

  • 对于“黄色”ImageButton,我将“红色”用于布局,将“黄色”用于测量。
  • 对于“蓝色”ImageButton,我得到“黄色”用于绘图。
  • 对于“绿色”TextView,我得到“黄色”的布局。
  • 对于内部RelativeLayout,我得到“红色”用于测量和绘图。
  • 对于外部 RelativeLayout,我得到“红色”用于测量,“黄色”用于布局和绘图。

有没有办法做我正在做的事情(即使用选择器作为背景和图像源)并且仍然更有效?

【问题讨论】:

  • 我认为您使用了太多嵌套的 RelativeLayouts。您可以删除不需要的布局。
  • 嵌套布局不利于性能。 singleRelativeLayout 就是您所需要的。
  • 为了清楚起见,我没有包括我的背景代码及其选择器。然而,我想要的是一对带有圆角的 ImageButton 和一个背景,它们都带有圆形的左上角。在这种情况下,我相信嵌套是必要的。对吗?
  • 可以在一个relativeLayout中管理。通过将红色设置为父布局的背景,并将图像按钮设置为其子布局。您还可以使用权重和权重总和来设计布局。
  • 我不明白。你是说FrameLayout 应该是ImageButton 的背景吗?这会错过“...”,即这个布局不仅仅包含外部RelativeLayout。另外我对布局中的权重和权重总和的理解是它们与效率并不真正兼容。

标签: android android-layout android-relativelayout hierarchyviewer


【解决方案1】:

您只能使用一个 RelativeLayout。像这样的东西(代码未验证):

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/some_red_background">

    <TextView
        android:id="@+id/green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/green_border"
        android:text="@string/green_string"/>

    <ImageButton
        android:id="@+id/yellow"
        tools:ignore="ContentDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/green"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="2dp"
        android:background="@drawable/selector_yellow_border"
        android:padding="5dp"
        android:src="@drawable/selector_yellow_icon"/>

    <ImageButton
        android:id="@+id/blue"
        tools:ignore="ContentDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/yellow"
        android:layout_above="@id/green"
        android:background="@drawable/selector_blue_border"
        android:padding="5dp"
        android:src="@drawable/blue_icon"/>


</RelativeLayout>

【讨论】:

  • 内部RelativeLayout 确实有背景(“blueyellow_border”),但左上角是圆形的(在配色方案中不可见)。
  • 然后,您可以将蓝色和黄色项目放入简单的LinearLayout。但是,如果您知道蓝黄色项目的大小,最好将具有必要背景的 View 添加到 RelativeLayout(在蓝色和黄色之前,用于 z-order)
  • 蓝色和黄色项目是矢量绘图,因此,我相信,它们的绝对大小没有给出。但是,LinearLayout 是可能的。它会比RelativeLayout 更有效吗?
  • 好的,我自己来回答最后一个问题:它[复杂] (stackoverflow.com/questions/4069037/…)。我对其进行了测试,发现在我的情况下,LinearLayoutRelativeLayout 效率高一点点(5/100 秒)。 @PetrovDimitri 感谢您的帮助!
猜你喜欢
  • 2022-01-25
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2012-01-16
  • 2011-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多