【发布时间】: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