浮动操作按钮现在可见

问题描述 投票:0回答:1

我有一个活动中显示的支持地图片段。但是,浮动操作按钮不可见。我尝试了很多方法,但我没有到达任何地方。

<FrameLayout
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:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    tools:context=".views.activities.MainActivity"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_current_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_location_on_white_24dp"
    app:elevation="4dp"
    android:visibility="visible"
    app:layout_anchorGravity="bottom|right|end"/>

val fragmentTransaction = mainAct.supportFragmentManager.beginTransaction()
        val mapFragment = mainAct.supportFragmentManager.findFragmentById(R.id.map_fragment) as SupportMapFragment
        fragmentTransaction.replace(R.id.main_container, mapFragment)
        fragmentTransaction.addToBackStack("Map Fragment")
        fragmentTransaction.commitAllowingStateLoss()
        mapFragment.getMapAsync(this.mainAct)

任何帮助是极大的赞赏

编辑:最终对我有用的是

val mapFragment = this.childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)
android kotlin floating-action-button
1个回答
0
投票

问题是您将片段添加两次,一次从xml添加,然后再添加到代码中。只需删除片段事务的代码,然后使用findFragmentById查找和更新片段。

val mapFragment = mainAct.supportFragmentManager.findFragmentById(R.id.map_fragment) as SupportMapFragment
mapFragment.getMapAsync(this.mainAct)

或者如果你想在代码中执行它,只需用frameLayout替换xml中的片段,将其命名为fragment_container并添加像这样的新片段

val mapFragment = SupportMapFragment() // use the apropriate constructor
fragmentTransaction.add(R.id.fragment_container, mapFragment)
© www.soinside.com 2019 - 2024. All rights reserved.