Android GoogleMap v2 被裁剪或未完全显示

标签 android google-maps google-maps-android-api-2 supportmapfragment

我正在观察 Google map 的意外行为。 问题是 map 使用支持 map fragment 从顶部和底部区域获取裁剪, 如果我使用全屏, map 是完全可见的,如图 1 所示,但是如果我应用 map fragment 高度或权重属性, map 不是完全可见的,如图 2 所示。

图片 1 的布局 xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.paki.venturedive.awtest.MapsActivity" />

图片 2 的布局 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.6"
    tools:context="com.paki.venturedive.awtest.MapsActivity" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello world"
    android:layout_weight="0.4" />

Java 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // 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);
}

在图 2 中,我(用户将)无法看到格陵兰地区上方的北冰洋,同样底部区域也会丢失,如图 3 所示 enter image description here .

有没有人遇到过这个问题或者有谁知道如何解决这个问题?

任何引用链接或提示将不胜感激。

Picture 1 , Picture 2 .

最佳答案

使用这个 fragment 。 我明白你的意思了,通过在线性/相对布局中获取 map fragment , map 从顶部和底部被切割,一些部分不可见。由于您希望 map 占据屏幕的 60%,一种可能的解决方案是,将 FrameLayout 中的 map fragment 的高度作为 match_parent。

现在,将 LinearLayout 中的 TextView 与父级作为 FrameLayout 并将其重力设置为底部,并使用 DisplayMetrics 将其高度动态设置为屏幕的 40%(或您的要求是多少)类(class)。通过这种方式,我们确保为 map 提供全屏,这样就不会剪切任何部分,并在 map 上方显示文本。我还附上了更新后的屏幕截图。

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_framecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"

        android:layout_height="match_parent" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:id="@+id/tv_main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fragment_map"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:text="Hello world" />

    </LinearLayout>
</FrameLayout>

您的 onCreate 中用于 DisplayMetrics 的 java 代码:

   DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            mylinearlayout.setMinimumHeight((int)(metrics.heightPixels * 0.4));

Screenshot updated image with full map on top

我认为这会有所帮助。

关于Android GoogleMap v2 被裁剪或未完全显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36348708/

相关文章:

android - 使用 ValueAnimator 在 LinearLayout 权重上同时运行动画

javascript - 如何对 Google Maps API 进行跨域 AJAX 调用?

iOS - 确定坐标是否在 GMSPolyline 内

android - 在 Google map fragment 中重新定位 MyLocation 按钮

android cardview 显示卡周围的边框

android - 从 Android/iOS 上的移动网站启动 Youtube 应用程序

java - 填充 ArrayAdapter - 类型 String[]

javascript - 在悬停链接或文本上显示谷歌地图

java - 类型转换为 Dalvik 格式失败 : Unable to execute dex: java. nio.BufferOverflowException

android - 谷歌地图应用程序不显示 map