Google Maps v3 MapView not taking all of parent view in Android

zun*_*ndi 5 android google-maps google-maps-api-3 android-layout

I'm using a fragment named GoogleMapFragment which displays a Google Map (v3) MapView. This is the layout that the fragment itself is using, which is then inserted into another layout:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

The parent layout is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Overview section -->
    <LinearLayout
        android:id="@+id/games_overview_wrapper"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- Details section -->
    <LinearLayout
        android:id="@+id/games_detail_wrapper"
        android:orientation="vertical"
        android:minHeight="@dimen/node_game_details_min_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- Map section -->
    <FrameLayout
        android:background="#d40707"
        android:id="@+id/games_location_fragment_wrapper"
        android:minHeight="@dimen/node_game_details_min_height"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Photos section -->
    <FrameLayout
        android:id="@+id/games_gallery_fragment_wrapper"
        android:minHeight="@dimen/node_game_details_min_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

各个“部分”永远不会同时可见,而是根据其他事件隐藏和显示。的game_location_fragment_wrapper是其中的MapView将被插入的布局。

在“游戏”片段中,我插入了 GoogleMapFragment:

// Setup the Google Maps fragment
if (node.hasLocation(getActivity())) {
    GoogleMapsFragment googleMapsFragment = new GoogleMapsFragment();
    googleMapsFragment.setNode(node);
    googleMapsFragment.addLocations(node);

    getChildFragmentManager()
            .beginTransaction()
            .replace(R.id.games_location_fragment_wrapper, googleMapsFragment, node.getValue(NODE_KEY.TITLE))
            .commit();
} else {
    locationIv.setVisibility(View.GONE);
    gamesLocationFragmentWrapper.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但 MapView 并没有占用所有父视图,然后在显示后的一瞬间导致java.lang.IllegalStateException: View size is too small after padding异常。

在此处输入图片说明

在屏幕截图中,红色背景表示包含视图的大小。MapView 显然不是全尺寸。

有小费吗?

更新:添加了地图片段的相关部分:

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_maps, container, false);

    mapView = (MapView)view.findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);

    return view;
}
Run Code Online (Sandbox Code Playgroud)

和...

@Override
public void onMapReady(final GoogleMap googleMap) {

    googleMap.setOnInfoWindowClickListener(this);

    final LatLngBounds.Builder bounds = new LatLngBounds.Builder();
    int count = 0;

    for(Node node : relatedNodes) {

        try {

            if (node.hasLocation(getActivity())) {

                final double latitude = Double.parseDouble(node.getValue(NODE_KEY.LATITUDE));
                final double longitude = Double.parseDouble(node.getValue(NODE_KEY.LONGITUDE));

                final LatLng latlng = new LatLng(latitude, longitude);
                bounds.include(latlng);

                final MarkerOptions markerOptions = new MarkerOptions().position(latlng).title(node.getValue(NODE_KEY.TITLE));

                Marker marker = googleMap.addMarker(markerOptions);

                markerNodeHashmap.put(marker, node);

                count++;
            }
        } catch (NumberFormatException e) {
            // Double wasn't parsed right. Continue.
        }
    }

    if (count > 0) {
        googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), UI.convertDpToPixel(getActivity(), 50)));
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 1

你可以检查这个页面,了解有关 java.lang.IllegalStateException: View size is toosmall after padding 的错误。

您还可以查看此页面以获取更多信息。