目前我有一个 LinearLayout
和一堆其他 View 作为 subview 。几乎所有的都使用 wrap_content
的高度,主要的 LinearLayout
也使用 wrap_content
。但是,主 LinearLayout
中的最后一个子级是使用 match_parent 的 RelativeLayout
。它包含一个 MapView
和一些按钮。
我想在最后一个 child 之前添加更多 child 。通过这样做,我添加的子项越多, map 可以占用的空间就越小。这可能会导致 map 根本不再可见,因为其他子项(即使使用 wrap_content
)也占据了整个空间。
我的想法或愿望是将主要的LinearLayout
放在ScrollView
中。然后我就可以拥有无限的 child 。
我的问题是:我怎样才能让我的RelativeLayout占据屏幕的其余部分或高度至少为200dp,这样我就可以确保它是可见的,即使没有足够的空间屏幕上有空间来首先显示它吗?
我的布局可能/应该如下所示:
<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/