【问题标题】:Floating action button now visible浮动操作按钮现在可见
【发布时间】:2017-12-24 17:40:01
【问题描述】:

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

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

【问题讨论】:

  • 请尝试使用 RelativeLayout 而不是 frameLayout,因为 FAB 位于片段下方...
  • 感谢您的回复。这对我不起作用。
  • FrameLayout 中有两个孩子吗?
  • 不,那是我的片段布局文件,底部有结束标签。

标签: android kotlin floating-action-button


【解决方案1】:

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

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

或者,如果您想在代码中执行此操作,只需将 xml 中的片段替换为 frameLayout 调用它 fragment_container 并像这样添加新片段

val mapFragment = SupportMapFragment() // use the apropriate constructor
fragmentTransaction.add(R.id.fragment_container, mapFragment)

【讨论】:

  • 感谢您的回复。当我出于某种原因尝试您的解决方案时,浮动操作按钮未显示。我假设它在布局下。所以我不得不使用一个子片段管理器在片段内有一个片段。
猜你喜欢
  • 2020-02-26
  • 2020-01-02
  • 1970-01-01
  • 2018-01-14
  • 2015-06-25
  • 2019-01-07
  • 2018-10-18
  • 2015-09-01
  • 2018-05-21
相关资源
最近更新 更多