2011-09-26 98 views
1

我應該使用什麼類型的佈局來實現這樣的佈局? enter image description here幫助佈局類型

我應該使用線性佈局還是相對的?你能否解釋你爲什麼選擇了你所做的佈局。樣本也會有幫助。

感謝

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <RelativeLayout android:layout_width="match_parent" 
     android:background="#ff00" android:id="@+id/relativeLayout1" 
     android:layout_height="100dp" android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"></RelativeLayout> 
    <RelativeLayout android:layout_width="match_parent" 
     android:background="#f0f0" android:id="@+id/relativeLayout2" 
     android:layout_height="wrap_content" android:layout_alignParentTop="true" 
     android:layout_above="@+id/relativeLayout1" 
     android:layout_centerHorizontal="true"></RelativeLayout> 
</RelativeLayout> 
1

如果您的佈局很簡單,只要在上面顯示,我會使用的LinearLayout - 主要是因爲我覺得相對佈局有點惱人,更難找到的bug工作

的樣本會。是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout android:layout_height="wrap_content" 
    android:layout_width="fill_parent" android:id="@+id/linearLayout1" 
    android:orientation="vertical" android:layout_weight="1" 
    android:background="@android:color/darker_gray"> 
     <!-- Put widgets here --> 
    </LinearLayout> 
    <LinearLayout android:layout_height="100dp" 
    android:layout_width="fill_parent" android:id="@+id/linearLayout2" 
    android:orientation="vertical" android:background="@android:color/background_light"> 
     <!-- Put widgets here --> 
    </LinearLayout> 
</LinearLayout> 
+0

夫婦的筆記,第一線性說,高度WRAP_CONTENT,並不意味着它會擴大,因爲內容被添加?第二條線是否位於屏幕的底部?它看起來就像坐在最下面的那個下面。 – dotty

+0

這裏的訣竅是第一個LinearLayout的layout_weight =「1」。這有助於告訴佈局佔用所有可用空間 - 無論其內容或自己的layout_height值如何。這也將第二個佈局定位在屏幕的底部,因爲它只佔用100dp的高度,剩下的部分用於第一個佈局。 – kaspermoerch