2012-12-30 29 views
0

主要想法是FrameLayout(黑色)居中且LinearLayout(紅色)對齊底部。這在我的4.x模擬器和我的Galaxy Nexus上完美工作。就像這樣:使用合併對齊Android 2.1時的問題

Good Layout

在Eclipse ADT佈局預覽看待各種規模的罰款。但是,在2.1仿真器上運行時,FrameLayout(黑色)似乎向下移動。

BadLayout

我得到了以下佈局:

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" > 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" > 

     <LinearLayout 
      android:layout_width="300dp" 
      android:layout_height="200dp" 
      android:layout_marginBottom="70dp" 
      android:background="#000000" > 

     </LinearLayout> 
    </FrameLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="70dp" 
     android:layout_gravity="bottom|center" 
     android:background="#FF0000" 
     android:orientation="horizontal" > 

    </LinearLayout> 

</merge> 

任何人有什麼造成這種任何想法?

+0

您是否嘗試過使用,而不是'' 「常規」 佈局(如RelativeLayout的)? – Nick

+0

我剛剛嘗試用RelativeLayout替換合併。我可以通過設置(黑色)layout_centerInParent和(紅色)alignParentBottom來獲得我想要的佈局。但是,這足以讓我完全相同的問題。我懷疑Android 2.1忽略了我在底部(黑色)的70dp餘量。 – ndsc

+0

哦,對不起,我沒有看到70dp的餘量。在這種情況下,由於他的LinearLayout能夠「識別」紅色條的存在並相應地將黑色框重新排列到「新」中心,因此gezdy的方法應該可行。 – Nick

回答

1

這應該工作:

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="300dp" 
      android:layout_height="200dp" 
      android:background="#000000" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="70dp" 
     android:layout_gravity="bottom" 
     android:background="#FF0000" /> 

</LinearLayout> 
+0

謝謝。這給了我想要的(以及2.1)。 – ndsc