2013-11-24 27 views

回答

0

如果我从你的插图中正确理解,红框是一个RelativeLayout,而绿框是一个LinearLayout和一个RelativeLayout。

一个简单的解决办法是居中的RelativeLayout内的空查看并对准两个子视图反对:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@+id/v_center" /> 

    <View 
     android:id="@+id/v_center" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@+id/v_center" /> 
</RelativeLayout> 

这里的一个不错的小好处是,你可以通过指定在两者之间提供一些空间视图的尺寸。

但是,请注意,RelativeLayouts效率不高,嵌套它们是一个特别糟糕的主意。我建议使用层次结构查看器工具来检查布局时间,以确保其速度相对较快,并尝试避免以这种方式嵌套布局。

1

这是很容易,使用参数layout_weight在LinearLayout中的孩子,这样的事情:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent"> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent"> 
</RelativeLayout> 
</LinearLayout>