java 如何在 Google Maps Android API v2 中向 MapFragment 添加自定义控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15136299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:36:59  来源:igfitidea点击:

How to add custom controls to MapFragment in Google Maps Android API v2?

javaandroidgoogle-mapsandroid-fragmentsgoogle-maps-android-api-2

提问by Tomá? Linhart

I have SupportMapFragment and I need to add custom controls into it for changing a map type. Calling getView(), I get NoSaveStateFramelayout and I don't think it is a good idea to add it directly into it or its children.

我有 SupportMapFragment,我需要向其中添加自定义控件以更改地图类型。调用 getView(),我得到 NoSaveStateFramelayout,我认为将它直接添加到它或其子项中不是一个好主意。

What is the best way how can I can add a button over my map for changing the map type?

如何在地图上添加按钮以更改地图类型的最佳方法是什么?

回答by Tomá? Linhart

I have decided to override onCreateView and encapsulate the map in the code.

我决定覆盖 onCreateView 并将地图封装在代码中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
    View mapView = super.onCreateView(inflater, viewGroup, bundle);
    RelativeLayout view = new RelativeLayout(getActivity());
    view.addView(mapView, new RelativeLayout.LayoutParams(-1, -1));
    // working with view
    return view;
}

And it works as I needed.

它可以按我的需要工作。

回答by Emil Adz

Try to put your map inside of a Frame Layout with Relative layout together when the map and the relative layout match_parent for the width and the height. In the relative layout place all your desired extended controls. That way the will sit on top on the map.

当地图和相对布局 match_parent 的宽度和高度时,尝试将地图放在具有相对布局的框架布局中。在相对布局中放置所有所需的扩展控件。这样,将坐在地图的顶部。

some thing like that:

类似的事情:

<FrameLayout 
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/title_gray_background" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:text="@string/tasks"
            android:paddingRight="3dp"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="@color/my_even_darker_gray" />

        <Button
            android:id="@+id/bLocateMe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            android:background="@drawable/locate_me_button_selector"
            android:clickable="true"
            android:onClick="locateMeButtonOnClick"
            android:text="@string/locate_me"
            android:textColor="@color/my_white" />

    </RelativeLayout>

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

</FrameLayout>

回答by I Love Coding

The view returned from onCreateView of MapFragment is a FrameLayout. So let use it, no need to add another layout (we will reduce view hierarchy).

MapFragment 的onCreateView 返回的视图是一个FrameLayout。所以让我们使用它,不需要添加另一个布局(我们将减少视图层次结构)。

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        FrameLayout mapView = (FrameLayout) super.onCreateView(inflater, viewGroup, bundle);
        mapView.add(someView);
        return view;
     }

回答by nv95

Use this code to add you layout above of the map

使用此代码在地图上方添加布局

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mapView = super.onCreateView(inflater, container, savedInstanceState);
    @SuppressLint("InflateParams")
    FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_map, null);
    view.addView(mapView, 0, new RelativeLayout.LayoutParams(-1, -1));
    return view;
}

回答by Quintin Balsdon

I did the following:

我做了以下事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!--suppress AndroidDomInspection -->

    <fragment
      android:id="@+id/map"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_alignParentBottom="true"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentRight="true"

    />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        >
    <ImageButton
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10sp"
            android:layout_marginTop="10sp"
            android:src="@android:drawable/ic_menu_mylocation"
            />
</RelativeLayout>