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

≡放荡痞女 提交于 2019-12-02 18:26:24

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

@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.

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.

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>

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).

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

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;
}

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>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!