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

后端 未结 5 1320
故里飘歌
故里飘歌 2021-02-01 08:38

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

相关标签:
5条回答
  • 2021-02-01 09:06

    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>
    

    0 讨论(0)
  • 2021-02-01 09:08

    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;
         }
    
    0 讨论(0)
  • 2021-02-01 09:18

    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>
    
    0 讨论(0)
  • 2021-02-01 09:26

    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.

    0 讨论(0)
  • 2021-02-01 09:31

    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;
    }
    
    0 讨论(0)
提交回复
热议问题