您尝试过FrameLayout吗?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/BlanchedAlmond" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/startActivityButton"
/>
<Button
android:id="@+id/startActivityButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:onClick="startActivity"
android:text="@string/start_activity"
android:layout_alignParentBottom="true" />
</FrameLayout >
我在应用程序中所做的是在地图显示时在顶部动态添加一个片段。
MD要求的代码和布局:
主要XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Turios" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/display_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#333"
android:paddingBottom="4dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="4dp"
android:textColor="#fff" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:background="@drawable/shape_background_darkgrey"
android:padding="5dp" />
</FrameLayout>
Mapoptions XML(我只是在LinearLayout中动态添加按钮):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:orientation="vertical"
android:paddingTop="65dp"
android:paddingRight="10dp" >
<LinearLayout
android:id="@+id/map_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
代码:
private FragmentTransaction addMapFragment(FragmentTransaction ft) {
if (mapsFragment == null) {
AddressHolder address = (device.isMultiPane()) ? null
: getSelectedAddressHolder();
mapsFragment = GoogleMapFragment.newInstance(address);
ft.add(R.id.details, mapsFragment, Turios.FRAGMENT_MAP_TAG);
mapsOptionsFragment = new GoogleMapOptionsFragment();
ft.add(R.id.details, mapsOptionsFragment, FRAGMENT_MAP_OPTIONS_TAG);
} else {
ft.attach(mapsFragment);
ft.attach(mapsOptionsFragment);
}
return ft;
}
@Override public void onTabSelected(Tab tab,
android.app.FragmentTransaction ft) {
int position = tab.getPosition();
FragmentTransaction t = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
if (position == DisplayFragment.NAVIGATION_INDEX) {
if (!device.isMultiPane() && mapsFragment != null
&& mapsOptionsFragment != null) {
t.detach(mapsFragment);
t.detach(mapsOptionsFragment);
}
if (browserFragment != null) {
t.detach(browserFragment);
}
}
if (position == GoogleMapFragment.NAVIGATION_INDEX) {
t = addMapFragment(t);
}
if (position == BrowserFragment.NAVIGATION_INDEX) {
if (browserFragment == null) {
browserFragment = new BrowserFragment();
t.add(R.id.details, browserFragment,
Turios.FRAGMENT_BROWSER_TAG);
} else {
t.attach(browserFragment);
}
}
t.commit();
}
@Override public void onTabUnselected(Tab tab,
android.app.FragmentTransaction ft) {
int position = tab.getPosition();
FragmentTransaction t = fm.beginTransaction();
if (position == GoogleMapFragment.NAVIGATION_INDEX) {
t.detach(mapsFragment);
t.detach(mapsOptionsFragment);
}
if (position == BrowserFragment.NAVIGATION_INDEX) {
t.detach(browserFragment);
}
t.commit();
}