谷歌地图调整大小android

时间:2015-11-14 16:52:23

标签: android

我有一张占据整个屏幕的地图。在标记点击时,我在地图视图下面添加按钮并缩小地图大小。这样做会使全屏地图闪烁并更改其尺寸。我该怎么办呢?

然后在标记点击下面的java代码添加按钮:

public void createMarkerMenu(MyLocation myLocation) {
    LinearLayout layout = (LinearLayout) findViewById(R.id.markerMenu);
    Button route = new Button(this);
    route.setText(myLocation.getEmail());
    layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(route);
}

xml结构如下:

<LinearLayout>
    <RelativeLayout>
    //map view
    </RelativeLayout>
    <LinearLayout>
    //empty. Adding button here from java file
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试浏览您的代码并使用以下提示,这有助于避免Google地图闪烁。经过研究,我发现这是一个常见的问题。

避免谷歌地图闪烁的错误:

不要做&#34;添加&#34;或&#34;替换&#34;使用包含Google地图片段的片段进行操作。 而是使用片段视图显示/隐藏操作。使用这些操作时,这不会重新创建已创建的片段。例如:

FragmentManager manager = getSupportFragmentManager();  
FragmentTransaction fT1 = manager.beginTransaction();
fT1.show (someFragments);
fT1.hide (someFragments2);
fT1.commit();  

在应用执行期间,请勿尝试更改Google地图片段尺寸参数width, height, margin, etc...。如果您需要一些谷歌地图动画,宽度更改等,请使用其他(其他)视图搜索变通方法。

答案 1 :(得分:0)

您可以使用这样的xml结构:

<RelativeLayout>
    //map view

    <LinearLayout>
        //empty. Adding button here from java file
    </LinearLayout>
</RelativeLayout>

例如:

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

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

    <LinearLayout
        android:id="@+id/markerMenu"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

由于我在xml的markerMenu版面上设置了宽度和高度,因此您可以从createMarkerMenu()方法中删除它:

public void createMarkerMenu(MyLocation myLocation) {
    LinearLayout layout = (LinearLayout) findViewById(R.id.markerMenu);
    Button route = new Button(this);
    route.setText(myLocation.getEmail());
    layout.addView(route);
}