Android 选项菜单在 Google 地图上显示不佳(Api v2)

Android Options menu not display well on Google Maps (Api v2)

在 activity 中,我有一个包含 google 地图的片段。在顶部我有一个按钮,如果单击它会显示一个选项菜单。 但是菜单显示不正确。它看起来是透明的,并且只显示三个选项中的两个。 你知道这种行为吗? 提前感谢您的任何回复。

minSdkVersion 15

这是我的菜单:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/zoom" android:title="Zoom sulla mia posizione" android:orderInCategory="1" />
    <item android:id="@+id/centra" android:title="Centra sulla mia posizione" android:orderInCategory="2" />
    <item android:id="@+id/mappa" android:title="Mappa iniziale" android:orderInCategory="3" />
</menu>   

这是我的 activity 地图布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_below="@+id/top_bar"
  android:layout_above="@+id/actnavbar"
  class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

这是我的 activity class (maps.class):

    package testapp;


import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class maps extends android.support.v4.app.FragmentActivity {

    private MyDatabase mDbHelper;
    private GoogleMap mMap;
    private LatLngBounds bounds;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_italia);
        setUpMapIfNeeded();

        ImageView menuButton = (ImageView) findViewById(R.id.menu_button); 
        menuButton.setOnClickListener(new OnClickListener() {           
            public void onClick(View v) {
//              Vibrator vibrazione = (Vibrator) getSystemService(getApplicationContext().VIBRATOR_SERVICE);
//              vibrazione.vibrate(25);         

                openOptionsMenu();              
            }
        });

    }

    @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 Activity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the Activity 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();
            //}
            mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {
                    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.setMapType(1);

        CameraUpdate update = null;

        //bound dei centri
        LatLngBounds.Builder builder = new LatLngBounds.Builder();

        Float latitudine = 39.40;
        Float longitudione = 20.10;
        LatLng coordinateCentro = new LatLng(latitudine, longitudine);
        mMap.addMarker(new MarkerOptions()
           .position(coordinateCentro)
           .title("test")
           .snippet("AAA, BBBB")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        builder.include(coordinateCentro);

        //setMarkerUserPosition();
        bounds = builder.build();

        int padding = 0; // offset from edges of the map in pixels
        update = CameraUpdateFactory.newLatLngBounds(bounds, padding);

        mMap.moveCamera(update);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){        
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_italia, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.zoom:
                zoom();             
                return true;
            case R.id.centra:
                centra();
                return true;
            case R.id.mappa:
                mappaIniziale();
                return true;
            default:
                return true;

        }
    }

    public void setMarkerUserPosition(){
        /*Setta il marker azzurro nella posizione attuale dell utente*/
        GPSTracker gps = new GPSTracker(Italia.this);
        if(gps.canGetLocation()){
             double myLatitude = gps.getLatitude();
             double myLongitude = gps.getLongitude();
             LatLng coordinateAttuali = new LatLng(myLatitude, myLongitude);
             mMap.addMarker(new MarkerOptions()
             .position(coordinateAttuali)
             .title("Posizione Attuale")
             //.snippet()
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
         }
    }

    private void zoom() {
        Log.d("Menu", "ZOOM-----");
        float myTilt = mMap.getCameraPosition().tilt;           //TILT indica quanto la visuale sia "schiacciata"--> default 0
        float myBearing = mMap.getCameraPosition().bearing;     //BEARING indica la "rotazione" della visuale--> default 0
        GPSTracker gps = new GPSTracker(Italia.this);        
         // check if GPS enabled
         if(gps.canGetLocation()){
             double myLatitude = gps.getLatitude();
             double myLongitude = gps.getLongitude();            
             float myZoom = mMap.getCameraPosition().zoom;
             float newZoom;
             if ((myZoom + 4) < 20)
                 newZoom = myZoom + 4;
             else
                 newZoom = 20;

            //setta la giusta posizione(mia posizione) e livello di zoom iniziale della mappa
            LatLng mieCoordinate = new LatLng(myLatitude, myLongitude);
            CameraPosition camPos = new CameraPosition(mieCoordinate, (float) newZoom, myTilt, myBearing); 
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
          //stop GPS
            gps.stopUsingGPS();
         }else{
             // can't get location  // GPS or Network is not enabled  // Ask user to enable GPS/network in settings
             gps.showSettingsAlert();
         }
    }

    private void centra() {
        Log.d("Menu", "CENTRA-----");
        float myZoom = mMap.getCameraPosition().zoom;
        float myTilt = mMap.getCameraPosition().tilt;           //TILT indica quanto la visuale sia "schiacciata"--> default 0
        float myBearing = mMap.getCameraPosition().bearing;     //BEARING indica la "rotazione" della visuale--> default 0
        GPSTracker gps = new GPSTracker(Italia.this);        
         // check if GPS enabled
         if(gps.canGetLocation()){
            double myLatitude = gps.getLatitude();
            double myLongitude = gps.getLongitude();            



            //setta la giusta posizione(mia posizione) e livello di zoom iniziale della mappa
            LatLng mieCoordinate = new LatLng(myLatitude, myLongitude);
            CameraPosition camPos = new CameraPosition(mieCoordinate, myZoom, myTilt, myBearing); 
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
            //stop GPS
            gps.stopUsingGPS();
         }else{
            // can't get location   // GPS or Network is not enabled  // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
         }
    }

    private void mappaIniziale()
    {
        Log.d("Menu", "MAPPAINIZIALE***-----");
        /*LatLng coordinateRoma = new LatLng(42.5, 12.483333);
        CameraPosition camPos = new CameraPosition(coordinateRoma, (float) 5, (float) 0, (float) 0); 
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));*/
        mMap.setMapType(1);
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,0));
    }

}

请尝试让你的activityclass继承自ActionBarActivityActivity而不是FragmentActivity,即important

此外,您可以按如下方式更改 AppTheme

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

它将显示 ActionBar 其中包含一个 menu

此外,您可以参考我的 Github here.

中的示例源代码

您想要一个带有操作栏的 FragmentActivty。如果使用 appcompact,你 must 使用 ActionBarActivity。不用担心 FragmentActivty 已经包含在 ActionBarActivity 中了。

Ps:这只是我的意见,但如果您想要一个完整的可自定义操作栏,请考虑使用工具栏。使用 appcompact,您不能将其用于您正在使用的最低 SDK。