【问题标题】:How to add custom controls to MapFragment in Google Maps Android API v2?如何在 Google Maps Android API v2 中向 MapFragment 添加自定义控件?
【发布时间】:2015-11-16 22:36:45
【问题描述】:

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

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

【问题讨论】:

    标签: java android google-maps android-fragments google-maps-android-api-2


    【解决方案1】:

    我决定重写 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;
    }
    

    它可以按我的需要工作。

    【讨论】:

      【解决方案2】:

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

      类似的东西:

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

      【讨论】:

      • 感谢您的回答,但最后我想出了自己的解决方案。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          我做了以下事情:

          <?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>
          

          【讨论】:

            猜你喜欢
            • 2013-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多