2013-11-24 25 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>