无法更改MapView高度

时间:2017-05-12 19:31:21

标签: java android xml google-maps android-mapview

因为它在标题中 - 我无法更改MapView高度。 我在说什么:

Screen 1 | Screen 2

fragment_map_view.xml 位于 fragment_main_view.xml 中,位于 main_activity.xml viewpager中。

fragment_map_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/startview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</ScrollView>

fragment_main_view.xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.trattoria.restaurant_app.activities.MainFragmentActivity"
    tools:ignore="MergeRootFrame"/>

main_activity:

<com.trattoria.restaurant_app.widgets.TouchCallbackLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@id/header"
        android:background="@color/colorPrimary"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:gravity="center_vertical"
            android:paddingLeft="@dimen/space_middle"
            android:paddingRight="@dimen/space_middle"
            android:textColor="@android:color/white"
            android:text="@string/header_text"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_width="match_parent"
            android:layout_height="@dimen/viewpager_header_height"/>

        <com.trattoria.restaurant_app.widgets.SlidingTabLayout
            android:id="@id/tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tabs_height"/>
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/tabs_height"/>

</com.trattoria.restaurant_app.widgets.TouchCallbackLayout>

MapViewFragment.java:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.CameraPosition;
import com.trattoria.restaurant_app.R;
import com.trattoria.restaurant_app.delegate.ScrollViewDelegate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.InputStream;

public class MapViewFragment extends BaseViewPagerFragment
        implements OnMapReadyCallback
{

    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;
    private ScrollView mScrollView;
    private ScrollViewDelegate mScrollViewDelegate = new ScrollViewDelegate();


    public static MapViewFragment newInstance(int index)
    {
        MapViewFragment fragment = new MapViewFragment();
        Bundle args = new Bundle();
        args.putInt(BUNDLE_FRAGMENT_INDEX, index);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        mView = inflater.inflate(R.layout.fragment_map_view, container, false);
        mScrollView = (ScrollView) mView.findViewById(R.id.startview);

        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        mMapView = (MapView) mView.findViewById(R.id.map);
        if(mMapView != null)
        {
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap)
    {
        MapsInitializer.initialize(getContext());

        mGoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(40.689247, -74.044502))
                .title("Statue of Liberty")
                .snippet("SAMPEL POJNT"));

        CameraPosition Liberty = CameraPosition.builder()
                .target(new LatLng(40.689247, -74.044502))
                .zoom(16)
                .bearing(0)
                .tilt(45)
                .build();

        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(Liberty));
    }

    @Override
    public boolean isViewBeingDragged(MotionEvent event)
    {
        return mScrollViewDelegate.isViewBeingDragged(event, mScrollView);
    }

    private String loadContentString()
    {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[8 * 1024];
        int len;
        String content = "";
        try
        {
            inputStream = getActivity().getAssets().open("start.txt");
            while ((len = inputStream.read(buf)) != -1)
            {
                outputStream.write(buf, 0, len);
            }
            content = outputStream.toString();
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            closeSilently(inputStream);
            closeSilently(outputStream);
        }

        return content;
    }

    public void closeSilently(Closeable c)
    {
        if (c == null)
        {
            return;
        }
        try
        {
            c.close();
        } catch (Throwable t)
        {
            // do nothing
        }
    }
}

我没有想法,我尝试的每件事都不起作用 - 当我尝试选择地图标签时,应用程序看起来相同或只是崩溃。谢谢你的帮助。

3 个答案:

答案 0 :(得分:0)

您可以手动设置布局高度和重量,如android:layout_width =&#34; 200dp&#34; 机器人:layout_height =&#34; 100dp&#34; 而不是使用匹配父。

答案 1 :(得分:0)

在你的fragment_map_view中,设置

android:fillViewPort="true"

滚动浏览器和地图将以完整布局显示。

答案 2 :(得分:0)

onResume()为我工作后设置身高

mapView.onResume()
mapView.layoutParams = ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.MATCH_PARENT,
                                    view.context.dpToPx(120f)
)