几乎填满整个屏幕的地图片段

Map fragment filling almost all the screen

我正在尝试在 MapFragment 和简单的 TextView 之间共享屏幕布局的高度,这样屏幕底部的小 TextView获取它需要的所有 space,而 GoogleMap 片段填充可用屏幕的其余部分。

像这样:

唯一的问题是我只能通过在 450dp 处静态指定片段的 android:layout_height 来获得这样的结果,而我还没有找到动态的方法,所以布局将适应其 landscape 模式和具有不同屏幕尺寸的手机。

这是我尝试做的事情:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent" tools:context="madapps.bicitourbo.DetailsFragmentTwo">

    <LinearLayout android:id="@+id/map"
        android:layout_height="wrap_content" android:layout_width="match_parent"
        android:orientation="vertical"
        android:weightSum="10">
        <!--android:paddingRight="16dp"-->
        <!--android:paddingLeft="16dp"-->

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapFrag"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="9"
            android:name="com.google.android.gms.maps.MapFragment"/>

        <TextView android:id="@+id/headerTitle"
            android:gravity="center_horizontal"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:text="Partenza da: Piazza Santo Stefano"
            android:textSize="@dimen/textSizeBig" android:textColor="@color/textLightBlack"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:layout_weight="1"/>
    </LinearLayout>

</ScrollView>

这是不想要的结果:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent" tools:context="madapps.bicitourbo.DetailsFragmentTwo">

<LinearLayout android:id="@+id/map"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:orientation="vertical"
    android:weightSum="10">
    <!--android:paddingRight="16dp"-->
    <!--android:paddingLeft="16dp"-->

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapFrag"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="9"
        android:name="com.google.android.gms.maps.MapFragment"/>

    <TextView android:id="@+id/headerTitle"
        android:gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Partenza da: Piazza Santo Stefano"
        android:textSize="@dimen/textSizeBig" android:textColor="@color/textLightBlack"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp" 
        android:layout_weight="1"/>
</LinearLayout>

android:weightSum 给出 LinearLayout 的总权重为 10。

因此 fragment 获得 LinearLayout 的 9/10,而 TextView 获得布局的 1/10。

你可以使用 RelativeLayout 这个

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="madapps.bicitourbo.DetailsFragmentTwo">

    <RelativeLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--android:paddingRight="16dp"-->
        <!--android:paddingLeft="16dp"-->

        <TextView
            android:id="@+id/headerTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="16dp"
            android:layout_marginTop="16dp"
            android:gravity="center_horizontal"
            android:text="Partenza da: Piazza Santo Stefano"
            android:textColor="@color/textLightBlack"
            android:textSize="@dimen/textSizeBig" />

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapFrag"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/headerTitle" />
    </RelativeLayout>

</ScrollView>

对我的问题的评论解决了它。

android:layout_weight 属性不起作用只是因为将 ScrollView 作为根标签会阻止其效果。 通过将 RelativeLayout 作为根标记,weigth 属性起作用,并且 TextView 应该根据需要向上扩展,同时地图会调整。