Invece di creare il proprio pulsante, basta spostare il pulsante di costruzione in base alla dimensione della barra di azione.
Questo codice funziona per me e il pulsante è proprio dove il tasto dovrebbe essere (come in google maps):
// Gets the my location button
View myLocationButton = getSherlockActivity().findViewById(R.id.MainContainer).findViewById(2);
// Checks if we found the my location button
if (myLocationButton != null){
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
// Checks if the os version has actionbar in it or not
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
if (getSherlockActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
// Before the action bar was added to the api
else if(getSherlockActivity().getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
// Sets the margin of the button
ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(myLocationButton.getLayoutParams());
marginParams.setMargins(0, actionBarHeight + 20, 20, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
myLocationButton.setLayoutParams(layoutParams);
}
Basta inserire questo codice nel onActivityCreated (se si vuole mettere nel onCreateOptionsMenu, non supporta la versione 3.0 prima - perché il ciclo di vita non è diversa
Un'altra cosa, il "R.id.MainContainer" è il contenitore del frammento mappa
sto usin.. g ActionBar Sherlock, ma funzionerà anche per la normale barra delle azioni con alcune modifiche ..
fonte
2013-04-01 08:28:40
http://stackoverflow.com/a/20750956/1066839 – John