【发布时间】:2014-03-10 15:25:24
【问题描述】:
我不是 Android 专家,但我知道有关适当使用 LinearLayout 和 RelativeLayout、保持视图层次结构尽可能小、避免不必要的 onMeasure() 传递等方面的讨论。
假设我有两个 ImageView,我想完全独立地定位,第一个在父级的中心,第二个在父级的左下角。 (请注意,这是一个大大简化的示例,说明了更复杂的现实生活需求)。
解决这个问题的明显方法是使用RelativeLayout...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/first_image" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="true"
android:layout_alignBottom="true"
android:src="@drawable/second_image" />
</RelativeLayout>
但是,有些东西一直告诉我,在这种情况下,RelativeLayout 不合适,因为我不想相对于彼此组织孩子。我想做的就是根据父级定位子级,我想知道使用 RelativeLayout 是否会导致一些我并不真正需要的不必要的布局计算。
我想知道是否还有另一种表现更好的 ViewGroup 类型?例如,它完全有可能通过 FrameLayout 实现我想要的,但我不知道这是否更高效,或者我是否在滥用 FrameLayout 等的意图......
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/first_image" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="@drawable/second_image" />
</FrameLayout>
也许还有另一种我不知道的布局类型?
【问题讨论】:
标签: android performance android-layout