【问题标题】:Android Google Maps FragmentAndroid 谷歌地图片段
【发布时间】:2017-07-20 17:33:24
【问题描述】:

我创建了一个新的 Android 项目,其 Google 地图活动定义为

public class MapsActivity extends FragmentActivity implements   OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

布局是这样的

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alewe.myapplication.MapsActivity" />

问题是: 我创建了另一个活动,并尝试在其中添加谷歌地图片段。

使用

FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());

不起作用,因为我无法将 FragmentActivity 转换为 Fragment。

如何添加刚刚在另一个活动中创建的谷歌地图片段? 谢谢

【问题讨论】:

  • 为什么不为另一个 Activity 创建另一个 SupportMapFrament 实例?
  • 如果我这样做,我可以在我创建的新活动中添加更多片段吗?
  • 有些应用由十个 Fragment 组成,全部显示在一个 Activity 中。正如你可以想象的那样,并不是所有的都同时出现。但是可以同时显示几个片段。您必须将 Activity 布局的某一部分分配给一个 Fragment,而将另一部分分配给下一个 Fragment Useful link
  • 谢谢。如果我将 SupportMapFrament 用于其他活动,我该如何选择插入片段的布局?
  • 也许我在那里误解了你,但听起来你想将片段从一个活动转移到下一个活动。只是永远不要这样做,这在技术上可能是可行的(毕竟我们正在编写 Java 代码),但从 android 的角度来看,这是绝对不行的。只需在您想使用它们的地方创建(视图和)片段。

标签: java android google-maps android-fragments


【解决方案1】:

像这样改变你的布局(activity_maps):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.alewe.myapplication.MapsActivity"
  android:id="@+id/main_view_map">

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      android:id="@+id/button"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:elevation="10dp"
      app:fabSize="mini"
      app:srcCompat="@drawable/ic_refresh_black_24dp"
      android:layout_marginTop="16dp"
      android:layout_marginEnd="16dp" />

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

不要使用

FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.firstLayout,this,this.getTag());

这段代码。

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 2015-06-10
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多