Android 如何在活动布局中包含碎片活动布局?

Android 如何在活动布局中包含碎片活动布局?,android,android-fragments,android-activity,android-fragmentactivity,Android,Android Fragments,Android Activity,Android Fragmentactivity,我想在另一个AppCompatActivity布局中包含一个片段活动布局 特别是,我想在另一个AppCompatActivity的容器中显示一个Google地图(在本例中为FragmentActivity)。 我要展示我的代码 1。AppCompative活动布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.

我想在另一个AppCompatActivity布局中包含一个片段活动布局

特别是,我想在另一个AppCompatActivity的容器中显示一个Google地图(在本例中为FragmentActivity)。 我要展示我的代码

1。AppCompative活动布局:

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

    }
}
3.碎片活动布局:

<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>
<?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" />
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
放在
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">
               <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>

我在
地图活动
中没有看到您的地图布局。此外,您在
片段活动
中使用地图,而不是
片段
。我会这样做的

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
显示地图。这将在碎片准备好之前使其膨胀,从而为您以后节省一些麻烦

这仍然给了我一个空白片段。片段视图中用于呈现FragmeComposativity布局的引用在哪里?请稍候。。。您是否遵循了设置地图的初始设置?是。。我已经拥有的地图是android studio的默认地图。我指的是
API键
等等。是的,你是对的。。。但是是的,我已经这样做了。当我直接启动映射时,我可以看到映射。是的,但是FragmentActivity类中的onCreate呢?您不需要FragmentActivity类,它是冗余的,这样您就可以直接从MapActivity控制所有内容,这样您就不会遇到上下文问题。MapFragment是为您提供的用于此确切目的的东西。为了显示GoogleMapok,我在乞求实现这样的解决方案时也有同样的想法,没有零碎的活动。但我认为是黑客。我更喜欢Documanuall的清晰解决方案,因为我想包括需要AppCompativeActivity的工具栏(应用程序栏)。不,这绝对不是黑客行为。这正是谷歌地图的用意。试试看,相信我。我在谷歌地图API上做了很多工作。