【发布时间】:2015-12-03 17:16:44
【问题描述】:
我想在另一个 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.AppCompatActivity 类
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.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.FragmentActivity 类:
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);
}
}
当我运行它时,只需在顶部显示应用栏,并在我需要地图的地方显示空白框。
请帮忙。
谢谢。
【问题讨论】:
标签: android android-fragments android-activity android-fragmentactivity