求助:MapView无法填充FrameLayout可用屏幕空间的问题排查
问题根源
ScrollView的测量逻辑决定了,它的直接子View设置android:layout_height="match_parent"不会生效——ScrollView会优先根据子View的内容高度计算尺寸,而非强制匹配自身可用空间。你的FrameLayout作为ScrollView的子View,match_parent实际等价于wrap_content,导致加载进来的CustomerMapResultsFragment布局只能包裹内容,最终MapView高度被压缩至仅能显示最小尺寸(约16dp)。
解决方案
根据是否需要滚动功能,有两种处理方式:
方式1:无需滚动时,直接移除ScrollView
如果容器内不会有超出屏幕的可滚动内容,直接让FrameLayout作为CoordinatorLayout的子View并保留滚动行为,这样FrameLayout会自动填充AppBar下方的所有空间:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragments.CustomerSearchFragment"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> ... </com.google.android.material.appbar.AppBarLayout> <FrameLayout android:id="@+id/fl_results_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
方式2:需要滚动时,替换为NestedScrollView并启用fillViewport
如果后续要添加可滚动内容,将ScrollView替换为NestedScrollView(与AppBarLayout的滚动行为兼容性更好),并添加android:fillViewport="true"属性,强制子View填充整个视口高度:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragments.CustomerSearchFragment"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> ... </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"> <FrameLayout android:id="@+id/fl_results_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
内容的提问来源于stack exchange,提问作者mik3.rizzo
相关产品推荐
相关产品推荐

