13

SupportMapFragment があり、マップ タイプを変更するためにカスタム コントロールを追加する必要があります。getView() を呼び出すと、NoSaveStateFramelayout が返されますが、それを直接またはその子に追加することはお勧めできません。

マップ タイプを変更するためにマップ上にボタンを追加するにはどうすればよいですか?

4

5 に答える 5

37

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

そして、それは私が必要としていたように機能します。

于 2013-03-02T14:24:20.167 に答える
5

マップと相対レイアウトの幅と高さが 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>
于 2013-02-28T23:42:57.190 に答える
1

このコードを使用して、マップの上にレイアウトを追加します

@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;
}
于 2015-11-16T14:36:45.877 に答える
1

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;
     }
于 2015-08-11T03:09:05.193 に答える
0

私は次のことをしました:

<?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-10-28T11:44:59.717 に答える