繁体   English   中英

浮动动作按钮现在可见

[英]Floating action button now visible

我有一个活动中显示的支持地图片段。 但是,浮动操作按钮不可见。 我已经尝试了很多方法,但是却一无所获。

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

问题是您将片段添加了两次,一次是从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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM