ScrollView 中的 LinearLayout:使最后 child 具有 minHeight 并在必要时填充空 space

LinearLayout in ScrollView: Make last child have minHeight and fill empty space if necessary

目前我有一个 LinearLayout 和一堆其他视图 children。几乎所有的都使用了wrap_content的高度,主要的LinearLayout也使用了wrap_content。但是,主 LinearLayout 中的最后一个 child 是一个使用 match_parent 的 RelativeLayout。它包含一个 MapView 和几个按钮。

我想在最后一个child之前再补充一些children。通过这样做,我添加的 children 越多,地图可以占用的 space 就会越来越小。这可以走这么远,以至于地图根本不再可见,因为其他 children,即使有 wrap_content,也占据了整个 space.

我的想法或愿望是将主要 LinearLayout 放在 ScrollView 中。那我就可以无限children了。

我的问题是:如何让我的 RelativeLayout 占据屏幕的其余部分或高度至少为 200dp,以便我可以确保它甚至可见如果一开始屏幕上没有足够的 space 来显示它?

我的布局 might/shall 看起来像这样:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout android:layout_height="wrap_content" ... >

        <LinearLayout android:layout_height="wrap_content" ... >

        <LinearLayout android:layout_height="wrap_content" ... >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.google.android.gms.maps.MapView
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <Button ... />
            <Button ... />
            <Button ... />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

试试下面的代码,它会对你有所帮助。

<ScrollView
       android:layout_width="match_parent"
      **android:fillViewport="true"**
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout android:layout_height="wrap_content" ... >

            <LinearLayout android:layout_height="wrap_content" ... >

            <LinearLayout android:layout_height="wrap_content" ... >

            <RelativeLayout
                android:layout_width="match_parent"
               **android:layout_height="0dp"
                android:layout_weight="1"**>

                <com.google.android.gms.maps.MapView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <Button ... />
                <Button ... />
                <Button ... />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

使用这个,你必须同时使用 dp 和 weight,你应该使用 weight 因为 当你的 child 较少时,你希望你的 RealtiveLayout 覆盖空的 space, 和 dp 当你的 child 很多那么你也想为你的 RelativeLayout

保持一个大小
<ScrollView
       android:layout_width="match_parent"
      android:fillViewport="true"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout android:layout_height="wrap_content" ... >

            <LinearLayout android:layout_height="wrap_content" ... >

            <LinearLayout android:layout_height="wrap_content" ... >

             <!--Use dp also, because when you have more childs, 
               it wants a dp to maitain a size-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_weight="1">

                <com.google.android.gms.maps.MapView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <Button ... />
                <Button ... />
                <Button ... />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>