2011-03-31 88 views
5

我想將屏幕分成2個部分。以上部分是一個地圖小部件。以下是其他佈局填充小部件。問題是地圖部件填滿了整個屏幕。我怎樣才能使地圖推入地圖下的佈局?Android佈局優先

我猜這可以通過給地圖小部件一個高度來解決,但這不是很靈活。

我學嘗試:

<?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="fill_parent"> 



    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:apiKey="my api key" /> 





    <RelativeLayout 
     android:id="@+id/map_menu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/mapview" 
     android:background="@drawable/locatie_background" > 


     <Button android:id="@+id/firstButtonId" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="@string/action_ready" 
      /> 

    </RelativeLayout > 

</RelativeLayout> 
+0

在RelativeLayout中試試這個:android:layout_weight =「1」。我認爲它會解決你的問題 – 2011-03-31 11:48:56

+0

@Kartik不,它不會。 'RelativeLayout'忽略'layout_weight'。 – 2011-03-31 11:52:48

+0

感謝您的提示。試圖改變到父佈局linearLayout,所以我可以分配一個權重到第二個相對佈局。但它給出了相同的結果 – Vincent 2011-03-31 11:54:10

回答

1

相反的RelativeLayout的使用LinearLayout中是這樣的:

<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:id="@+id/map_menu1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    > 

    <com.google.android.maps.MapView 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:clickable="true" 
    android:apiKey="my api key" /> 
</LinearLayout > 

<LinearLayout 
    android:id="@+id/map_menu" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    > 

    <Button android:id="@+id/firstButtonId" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="action_ready" 
     /> 

</LinearLayout > 

</LinearLayout> 
+0

謝謝,這就是我要找的 – Vincent 2011-03-31 12:09:23

+4

從技術上講,這是正確的,但不必要的第二個'LinearLayout'值得-1。 – 2011-03-31 12:15:07

+0

@karthi octavian是正確的..額外的記憶.. – 2011-03-31 13:01:41

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="wrap_content" 
android:orientation="vertical"> 



<com.google.android.maps.MapView 
    android:id="@+id/mapview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:apiKey="my api key" /> 





<RelativeLayout 
    android:id="@+id/map_menu" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/mapview" 
    android:background="@drawable/locatie_background" > 


    <Button android:id="@+id/firstButtonId" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="@string/action_ready" 
     /> 

</RelativeLayout > 

</LinearLayout> 
+0

這不起作用。 – 2011-03-31 11:58:22

+0

它會工作...我檢查了它 – Udaykiran 2011-03-31 12:01:43

+0

我猜我錯了問題。抱歉。 – 2011-03-31 12:03:51

4

去一個LinearLayout,設置兩個孩子的layout_heightwrap_content和設定兩者的layout_weight1

這將平分屏幕高度:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:apiKey="my api key" /> 
    <RelativeLayout 
     android:id="@+id/map_menu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_below="@id/mapview" 
     android:background="@drawable/locatie_background"> 
     <Button android:id="@+id/firstButtonId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/action_ready" /> 
    </RelativeLayout > 
</LinearLayout> 

您可以layout_weight工作,就好像它們是百分比值,喜歡這裏:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <com.google.android.maps.MapView 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="90" 
     android:clickable="true" 
     android:apiKey="my api key" /> 
    <RelativeLayout 
     android:id="@+id/map_menu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:layout_below="@id/mapview" 
     android:background="@drawable/locatie_background"> 
     <Button android:id="@+id/firstButtonId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/action_ready" /> 
    </RelativeLayout > 
</LinearLayout> 
+1

當我得到這個我得到錯誤:整數類型不允許(在'layout_height'值'0')。 – Vincent 2011-03-31 12:01:33

+0

糾正了我的錯誤。 – 2011-03-31 12:12:54

+0

我對此有另一個問題。我試着在第二個佈局中放置10個按鈕,而不是1個,但是看起來佈局不能被分割得多於其背景的高度。這是正常的嗎? – Vincent 2011-03-31 12:20:09