Android 动态插入片段时元素重叠

Android elements overlapping when dynamically inserting fragment

我的项目包含一个主 activity,当按下底部导航栏上的选项卡时,它会将不同的片段加载到视图中。蓝色表示 activity 控件,橙色表示其中一个片段应包含的内容。在这种情况下,它是一个 MapBox MapView,角落里有 FAB。

我的问题是我似乎无法获得正确的片段容器高度;顶部或底部按钮(有时两者都有)始终在状态栏或底部导航栏下方垂直截断。

我当前的布局(为简洁起见删除了不相关的属性):

activity_main.xml:

<ConstraintLayout
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/nav_bottom">
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_height="wrap_content">
        </FrameLayout>
    </RelativeLayout>

    <BottomNavigationView
        android:id="@+id/nav_bottom"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/nav_bottom">
    </BottomNavigationView>

</ConstraintLayout>

fragment_map.xml:

<ConstraintLayout
    android:layout_height="wrap_content">

    <MapView
        android:id="@+id/map_view"
        android:layout_height="match_parent"
        mapbox:layout_constraintTop_toTopOf="parent"
        mapbox:layout_constraintBottom_toBottomOf="parent">
    </MapView>

    <FloatingActionButton
        android:id="@+id/button_top"
        android:layout_height="wrap_content"
        mapbox:layout_constraintTop_toTopOf="@id/map_view"
        mapbox:layout_constraintRight_toRightOf="@id/map_view" />

    <FloatingActionButton
        android:id="@+id/button_bottom"
        android:layout_height="wrap_content"
        mapbox:layout_constraintBottom_toBottomOf="@id/map_view"
        mapbox:layout_constraintRight_toRightOf="@id/map_view" />

</ConstraintLayout>

我正在将片段添加到 activity,如下所示:

getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.fragment_container, fragment, "myFrag")
        .commit();

更改布局样式activity_main.xml:

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_height="0dp"
        weight = "1">

    </FrameLayout>

    <BottomNavigationView
        android:id="@+id/nav_bottom"
        android:layout_height="wrap_content"
        app:menu="@menu/nav_bottom">

    </BottomNavigationView>

</LinearLayout>

和你的fragment_map.xml:

<RelaytiveLayout
    android:layout_height="match_parent">

    <MapView
        android:id="@+id/map_view"
        android:layout_height="match_parent">

    </MapView>

    <FloatingActionButton
        android:id="@+id/button_top"
        android:layout_height="wrap_content"
        alignParentRight = true />

    <FloatingActionButton
        android:id="@+id/button_bottom"
        android:layout_height="wrap_content"
        alignParentRight = true
        alignParentBottom = true />

</RelaytiveLayout>

编辑了您的fragment_map.xml查看更改