安卓布局优先级

5

我正在尝试将屏幕分成两个部分。上面的部分是一个地图小部件,下面是另一个填充了小部件的布局。问题在于地图小部件占据了整个屏幕。如何使得地图下方的布局向上推动地图呢?

我猜可以通过给地图小部件设置高度来解决,但这并不是很灵活。

我的尝试:

<?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>

试一下在RelativeLayout中使用android:layout_weight="1"。我认为它可以解决你的问题。 - Kartik Domadiya
@Kartik 不会的。layout_weightRelativeLayout 中被忽略。 - Octavian Helm
你为什么不使用LinearLayout?你想把地图向上推多少? - ernazm
我希望底部区域的高度足够高,以显示其内容(几个按钮和一张图片)。 - Vincent
不,我希望顶部屏幕的高度根据底部内容进行缩放(底部优先)。 - Vincent
显示剩余2条评论
3个回答

4
使用LinearLayout,将两个子元素的layout_height都设置为wrap_content,并将两个子元素的layout_weight都设置为1。这样可以使屏幕高度平均分配。
这将平均分配屏幕高度。
<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
当我尝试这个时,我得到了错误:不允许整数类型(在'value'为'0'的'layout_height'处)。 - Vincent
我有另一个关于这个问题的问题。我尝试在第二个布局中放置10个按钮,而不是1个,但似乎布局不能被拉伸超过其背景的高度。这正常吗? - Vincent
如果你有很多选项可供选择,我会选择一个“Spinner”。 - Octavian Helm

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>

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>

4
从技术上讲是正确的,但不必要的第二个“LinearLayout”会被扣除1分。 - Octavian Helm
看看我的答案,你就会知道正确的做法。 - Octavian Helm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接