在一项活动中,我有一个包含谷歌地图的片段。在顶部,我有一个按钮,如果点击显示一个选项菜单。 但是菜单显示不正确。它看起来是透明的,只显示三个选项中的两个。 你对这种行为有什么想法吗? 如果有任何回复,请提前致谢。
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>
这是我使用地图进行活动的布局。
<?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>
这是我的活动类(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));
}
}
答案 0 :(得分:3)
请尝试让您的活动类继承自ActionBarActivity
或Activity
而不是FragmentActivity
,important
。
另外,您可以按如下方式更改AppTheme
:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
它会显示其中包含ActionBar
的{{1}}。
此外,您可以从我的Github here中引用示例源代码。
答案 1 :(得分:2)
你想要一个带有操作栏的FragmentActivty。如果使用appcompact,则must
使用ActionBarActivity
。不要担心FragmentActivty已经包含在ActionBarActivity中。
Ps:这只是我的意见,但是如果你想要一个完整的可操作的操作栏,请考虑使用工具栏。使用appcompact,您无法将其用于您正在使用的最小SDK。