如何在 Activity 布局中包含 FragmentActivity 布局?

How to include FragmentActivity layout in Activity layout?

我想在另一个 AppCompatActivity 布局中包含一个 FragmentActivity 布局。

我特别想在另一个 AppCompatActivity 的容器内显示一个 Google 映射(在本例中为 FragmentActivity)。 我要展示我的代码。

1. AppCompatActivity 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/map_toolbar"
        layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

2.The AppCompatActivity class

public class MapActivity extends AppCompatActivity {

    private Toolbar _toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
        setSupportActionBar(_toolbar);
    }
}

3.The FragmentActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

4.The片段活动class:

public class MapFragmentActivity extends FragmentActivity implements OnMapReadyCallback
{

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);

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

    }
}

当我 运行 这只是在顶部显示应用栏,在我需要地图的地方显示空白框。

请帮忙。

谢谢。

MapFragment 放入 AppCompatActivityLayout 中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/map_toolbar"
            layout="@layout/app_bar" />

        <LinearLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
               <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>

我在你的 MapActivity 中没有看到你的地图布局。此外,您在 FragmentActivity 而不是 Fragment 中使用您的地图。我就是这样做的。

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
    private Toolbar _toolbar;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
        setSupportActionBar(_toolbar);

        initMap();
    }

    public void initMap() {
        MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        map.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        try {
            if (googleMap != null) {
                mGoogleMap = googleMap;                             
                mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);               
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ERROR", "GOOGLE MAPS NOT LOADED");
        }
    }
}

然后我会在 XML:

中这样做
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/map_toolbar"
        layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

    </LinearLayout>
</LinearLayout>

那么您甚至不需要 MapFragmentActivity,因为它有点多余。此外,您现在正在等待地图准备就绪,然后再使用 implements OnMapReadyCallback 显示它。这将在片段准备好之前为片段充气,从而为您省去一些麻烦。