Android Google地图无法使用幻灯片菜单

时间:2015-10-21 09:32:18

标签: android google-maps drawerlayout

我有一个使用gmap从webservice获取标记并在地图上显示它们的应用程序。 这是非常好的布局代码

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.fakeyourlocation.map.LocationChooser">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:weightSum="10">
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hotel"
            android:text="Hoteli"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hostel"
            android:text="Hosteli"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/apartmani"
            android:text="Apartmani"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
    </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:weightSum="10">
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hotel1"
                android:text="Hoteli"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hostel2"
                android:text="Hosteli"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/apartmani2"
                android:text="Apartmani"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
        </TableRow>
    </LinearLayout>
    <Button
        android:layout_width="100dp"
        android:layout_height="70dp"
        android:layout_gravity="center|bottom"
        android:id="@+id/button2"
        android:text="Show my current location"
        android:padding="10dp"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pocisti"
        android:layout_gravity="left|bottom"
        android:text="Ukloni oznake"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:paddingRight="10dp"/>

    </fragment>

而不是主屏幕中的开关我想添加到幻灯片菜单。一旦我将幻灯片菜单的代码添加到此文件的末尾,我就可以看到幻灯片菜单,但谷歌地图停止工作。这是无效的新代码

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.fakeyourlocation.map.LocationChooser">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:weightSum="10">
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hotel"
            android:text="Hoteli"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hostel"
            android:text="Hosteli"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/apartmani"
            android:text="Apartmani"
            android:padding="10dp"
            android:layout_marginTop="20dp"
            android:paddingRight="10dp"/>
    </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:weightSum="10">
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hotel1"
                android:text="Hoteli"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/hostel2"
                android:text="Hosteli"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/apartmani2"
                android:text="Apartmani"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:paddingRight="10dp"/>
        </TableRow>
    </LinearLayout>
    <Button
        android:layout_width="100dp"
        android:layout_height="70dp"
        android:layout_gravity="center|bottom"
        android:id="@+id/button2"
        android:text="Show my current location"
        android:padding="10dp"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pocisti"
        android:layout_gravity="left|bottom"
        android:text="Ukloni oznake"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:paddingRight="10dp"/>
    <android.support.v4.widget.DrawerLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- As the main content view, the view below consumes the entire space available using match_parent in both dimensions. -->


    <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/content_frame"/>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat this as a sliding drawer on the left side for left-to-right languages and on the right side for right-to-left languages. The drawer is given a fixed width in dp and extends the full height of the container. A solid background is used for contrast with the content view. -->


    <ListView android:layout_height="match_parent" android:layout_width="240dp" android:id="@+id/left_drawer" android:background="#111" android:dividerHeight="0dp" android:divider="@android:color/transparent" android:choiceMode="singleChoice" android:layout_gravity="start"/>

</android.support.v4.widget.DrawerLayout>
    </fragment>

有关组合谷歌地图和幻灯片菜单的任何帮助

MapsActivity

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        ListView drawerView = (ListView) findViewById(R.id.left_drawer);

        List<String> switches = new ArrayList<String>();
        switches.add("Switch 1");
        switches.add("Switch 2");
        switches.add("Switch 3");

        ListAdapter adapter = new ListAdapter(this, R.layout.itemlistrow, switches);
        drawerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    public class ListAdapter extends ArrayAdapter<String> {
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public ListAdapter(Context context, int resource, List<String> items) {
            super(context, resource, items);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.itemlistrow, null);
            }

            String p = getItem(position);

            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.name);
                Switch switchI = (Switch) v.findViewById(R.id.value);

                if (tt1 != null) {
                    tt1.setText(p);
                }

                switchI.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        switch (position) {
                            case 1:
                                // TODO: Behaviour of switchI on first row
                                break;
                            case 2:
                                // TODO: Behaviour of switchI on second row
                                break;
                            default:
                                // TODO: Default behaviour
                                break;
                        }
                    }
                });
            }
            return v;
        }
    }
}

活性-maps.xml

<android.support.v4.widget.DrawerLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- As the main content view, the view below consumes the entire space available using match_parent in both dimensions. -->


    <FrameLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/content_frame"/>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat this as a sliding drawer on the left side for left-to-right languages and on the right side for right-to-left languages. The drawer is given a fixed width in dp and extends the full height of the container. A solid background is used for contrast with the content view. -->
    <ListView
        android:layout_height="match_parent"
        android:layout_width="140dp"
        android:id="@+id/left_drawer"
        android:background="#111"
        android:dividerHeight="0dp"
        android:divider="@android:color/transparent"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"/>
    <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.fakeyourlocation.map.LocationChooser">

    <Button
        android:layout_width="100dp"
        android:layout_height="70dp"
        android:layout_gravity="center|bottom"
        android:id="@+id/button2"
        android:text="Show my current location"
        android:padding="10dp"/>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pocisti"
        android:layout_gravity="left|bottom"
        android:text="Ukloni oznake"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:paddingRight="10dp"/>


    </fragment>
    </android.support.v4.widget.DrawerLayout>

itemlistrow.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="15dp"
        android:gravity="center_vertical"
        android:textColor="#fff"/>

    <Switch
        android:id="@+id/value"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textColor="#fff"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

来自the documentation

  

要添加导航抽屉,请使用a声明您的用户界面    DrawerLayout对象作为布局的根视图。在 - 的里面   DrawerLayout,添加一个包含主要内容的视图   屏幕(隐藏抽屉时的主要布局)和另一个   包含导航抽屉内容的视图

因此,android.support.v4.widget.DrawerLayout必须是根视图(在您的情况下,您只是在现有xml的末尾添加了它)。

这是一个带有map和DrawerLayout的工作xml的示例:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

</android.support.v4.widget.DrawerLayout>

更新:使用上面的答案和Custom Adapter for List View中的建议代码来举例说明左侧抽屉与开关的创建的简单示例:

<强> activity_maps.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#000"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

</android.support.v4.widget.DrawerLayout>

<强> itemlistrow.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="15dp"
        android:gravity="center_vertical"
        android:textColor="#fff"/>

    <Switch
        android:id="@+id/value"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textColor="#fff"/>

</LinearLayout>

<强> MapsActivity.java

public class MapsActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ListView drawerView = (ListView) findViewById(R.id.left_drawer);

        List<String> switches = new ArrayList<String>();
        switches.add("Switch 1");
        switches.add("Switch 2");
        switches.add("Switch 3");

        ListAdapter adapter = new ListAdapter(this, R.layout.itemlistrow, switches);
        drawerView.setAdapter(adapter);
    }

    public class ListAdapter extends ArrayAdapter<String> {
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public ListAdapter(Context context, int resource, List<String> items) {
            super(context, resource, items);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.itemlistrow, null);
            }

            String p = getItem(position);

            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.name);
                Switch switchI = (Switch) v.findViewById(R.id.value);

                if (tt1 != null) {
                    tt1.setText(p);
                }

                switchI.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        switch (position) {
                            case 1:
                                // TODO: Behaviour of switchI on first row
                                break;
                            case 2:
                                // TODO: Behaviour of switchI on second row
                                break;
                            default:
                                // TODO: Default behaviour
                                break;
                        }
                    }
                });
            }
            return v;
        }
    }
}

更新:xml,每行有两个开关

<强> itemlistrow.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:gravity="center_horizontal"
            android:text="SW1"
            android:textColor="#fff"/>

        <Switch
            android:id="@+id/value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:textColor="#fff"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView
            android:id="@+id/name2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:gravity="center_horizontal"
            android:text="SW2"
            android:textColor="#fff"/>

        <Switch
            android:id="@+id/value2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:textColor="#fff"/>

    </LinearLayout>

</LinearLayout>

或者,删除TextView并使用Switch

的text属性
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <Switch
            android:id="@+id/value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SW1"
            android:clickable="true"
            android:textColor="#fff"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <Switch
            android:id="@+id/value2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SW2"
            android:clickable="true"
            android:textColor="#fff"/>

    </LinearLayout>

</LinearLayout>

修改MapsActivity以在上面的同一行视图中使用两个开关(警告:未经过全面测试):

public class MapsActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ListView drawerView = (ListView) findViewById(R.id.left_drawer);

        List<RowItem> switches = new ArrayList<RowItem>();
        switches.add(new RowItem("Hotels", "Hostels");
        switches.add(new RowItem("Camps", "Private");

        ListAdapter adapter = new ListAdapter(this, R.layout.itemlistrow, switches);
        drawerView.setAdapter(adapter);
    }

    public class ListAdapter extends ArrayAdapter<RowItem> {
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        public ListAdapter(Context context, int resource, List<RowItem> items) {
            super(context, resource, items);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.itemlistrow, null);
            }

            RowItem p = getItem(position);

            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.name);
                Switch switchI1 = (Switch) v.findViewById(R.id.value);
                TextView tt2 = (TextView) v.findViewById(R.id.name2);
                Switch switchI2 = (Switch) v.findViewById(R.id.value2);

                tt1.setText(p.getSwitch1Name());
                tt2.setText(p.getSwitch2Name());

                switchI1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        switch (position) {
                            case 0:
                                // TODO: Behaviour of switchI1 on first row
                                break;
                            case 1:
                                // TODO: Behaviour of switchI1 on second row
                                break;
                            default:
                                // TODO: Default behaviour
                                break;
                        }
                    }
                });

                switchI2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        switch (position) {
                            case 0:
                                // TODO: Behaviour of switchI2 on first row
                                break;
                            case 1:
                                // TODO: Behaviour of switchI2 on second row
                                break;
                            default:
                                // TODO: Default behaviour
                                break;
                        }
                    }
                });
            }
            return v;
        }
    }

    private class RowItem {
        private String switch1Name;
        private String switch2Name;

        public RowItem(String switch1Name, String switch2Name) {
            this.switch1Name = switch1Name;
            this.switch2Name = switch2Name;
        }

        public String getSwitch1Name() {
            return switch1Name;
        }

        public String getSwitch2Name() {
            return switch2Name;
        }
    }
}