2011-03-31 104 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