2013-10-11 59 views
1

我已经创建了这个布局,它使用了几个嵌套的相对布局,我已经看到一些帖子说应该使用相对布局来代替嵌套线性布局。可以嵌套几个相对布局

如何嵌套相对布局?嵌套这样的布局有什么缺点(下面的xml代码)

如果有任何缺点,有谁能推荐一个更好的方法来做到这一点?提前致谢。

enter image description here

XML代码:

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

<TextView 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" /> 

<RelativeLayout 
    android:id="@+id/layout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/view1" 
    android:gravity="center" > 

    <TextView 
     android:id="@+id/view2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/view3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_toRightOf="@+id/view2" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/layout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/layout1" 
    android:gravity="center" > 

    <TextView 
     android:id="@+id/view4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/view5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_toRightOf="@+id/view4" /> 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/layout3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/layout2" 
    android:gravity="center" > 

    <TextView 
     android:id="@+id/view6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:id="@+id/view7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_toRightOf="@+id/view6" /> 
</RelativeLayout> 

</RelativeLayout> 

回答

1

在Google I/O 2013(为Android编写自定义视图)中的一次演讲中,Romain Guy澄清了误解,该误解导致每个人都开始使用RelativeLayouts来处理所有事情。一个RelativeLayout总是必须做两次测量。总体而言,只要您的视图层次结构很简单,它就可以忽略不计。但是如果你的层次结构很复杂,那么做一个额外的度量传递可能会相当昂贵。另外,如果你嵌套RelativeLayouts,你会得到一个指数测量算法。

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=1m41s

https://www.youtube.com/watch?v=NYtB6mlu7vA&t=38m04s

https://developers.google.com/events/io/sessions/325615129

可以测量使用了Android SDK工具,来到你的UI性能。看看这个链接:http://developer.android.com/training/improving-layouts/optimizing-layout.html

+0

你是什么意思通过测量通过? – AndroidEnthusiast

+0

在这种情况下,您可以考虑操作。因此,相对布局总是必须执行两个操作来计算其水平和垂直尺寸,并且因为LinearLayout总是具有固定方向,所以它只执行一个操作。 –

0

从我的理解有没有错这样做,虽然你应该避免嵌套布局尽可能。