java - 无法更改 MapView 高度

标签 java android xml google-maps android-mapview

正如标题所示 - 我无法更改 MapView 高度。 我在说什么:

Screen 1 | Screen 2

fragment_map_view.xml 位于 fragment_main_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"/>

主要 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
        }
    }
}

我没有主意了,我尝试的每件事都不起作用 - 应用程序看起来相同或只是在我尝试选择 map 选项卡时崩溃。感谢您的帮助。

最佳答案

在您的fragment_map_view中,设置

android:fillViewPort="true"

您的 ScrollView 和 map 将以完整布局显示。

关于java - 无法更改 MapView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43945194/

相关文章:

java - 实现通用类型接口(interface)

android - ionic Cordova 运行Android给出gradle错误

java - Android:共享菜单,我的菜单充气器无法解析我的资源

android - 为什么 "plus"在 Android 中引用名称中为 ID 资源签名

java - Android okhttp : Getting randomly java.net.ProtocolException : Unexpected status line: 1. 1 200 OK

xml - 如何返回 Marklogic 中元素范围索引中的所有元素

android - 使用 Google Maps Android API v2 时出现错误签名无效。发现 :

java - 模块所需的 Maven 项目配置不可用

java - Jboss中的spring boot返回错误404

java - Android 媒体播放器 : How to stop if another audio file is being loaded?