android - ScrollView 中的LinearLayout : Make last child have minHeight and fill empty space if necessary

标签 android scrollview android-linearlayout android-relativelayout android-mapview

目前我有一个 LinearLayout 和一堆其他 View 作为 subview 。几乎所有的都使用 wrap_content 的高度,主要的 LinearLayout 也使用 wrap_content 。但是,主 LinearLayout 中的最后一个子级是使用 match_parent 的 RelativeLayout。它包含一个 MapView 和一些按钮。

enter image description here

我想在最后一个 child 之前添加更多 child 。通过这样做,我添加的子项越多, map 可以占用的空间就越小。这可能会导致 map 根本不再可见,因为其他子项(即使使用 wrap_content)也占据了整个空间。

我的想法或愿望是将主要的LinearLayout放在ScrollView中。然后我就可以拥有无​​限的 child ​​。

我的问题是:我怎样才能让我的RelativeLayout占据屏幕的其余部分或高度至少为200dp,这样我就可以确保它是可见的,即使没有足够的空间屏幕上有空间来首先显示它吗?

enter image description here

我的布局可能/应该如下所示:

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

最佳答案

使用这个,你必须同时使用dp和权重,你应该使用权重,因为 当您的 child 较少时,您希望 RealtiveLayout 覆盖空白空间, 和 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>

关于android - ScrollView 中的LinearLayout : Make last child have minHeight and fill empty space if necessary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255697/

相关文章:

android - 在 RecyclerView 中设置 contentInset

android - 在 RecyclerView 中回收时,项目 View 未正确测量

android - 使用 ListView 和按钮限制 LinearLayout 的高度

java - 如何获得年龄并将其用于另一项 Activity ?

android - Firebase 移动应用程序跟踪 - 使用哪些库?

android - Retrofit 和 RecyclerView 在启动时不显示项目

android - 项目无法在模拟器上运行

android - 滚动自定义 View - Android

android - 使 subview 匹配父 ScrollView 的宽度

java - Recyclerview动态textview高度