So che è stato risposto e accettato ma queste risposte non ha funzionato per me. Forse ci sono stati cambiamenti in RelativeLayout.LayoutParams o anche in GoogleMaps da quando è stata fornita una risposta.
Nei miei tentativi con le risposte esistenti ho capito che potrebbero esserci più regole LayoutParam per il pulsante della bussola che stanno annullando i miei tentativi di spostare il pulsante della bussola dove lo volevo. Ho sperimentato rimuovendo diversi parametri come questo:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
E quindi aggiungere le nuove regole come hanno fatto in molte delle risposte. Ma ancora non ha funzionato come previsto. Più spesso che no, il pulsante è rimasto da qualche parte sul lato sinistro.
Ho deciso di creare solo nuovi layoutParams e sostituire quelli esistenti e ... ha funzionato perfettamente!
Originariamente ho utilizzato il tag vista per GoogleMapCompass
, ma la domanda riguardava lo GoogleMapMyLocationButton
. Così ho commentato la linea GoogleMapCompass
e inserito la linea GoogleMapMyLocationButton
.
Qui è il codice per MapFragment.java:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
private SupportMapFragment mapFragment = null;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
Log.d(TAG, "onCreateView()");
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.setRetainInstance(true);
mapFragment.getMapAsync(this);
return view.findViewById(R.id.map);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady()");
View mapView = mapFragment.getView();
moveCompassButton(mapView);
}
/**
* Move the compass button to the right side, centered vertically.
*/
public void moveCompassButton(View mapView) {
try {
assert mapView != null; // skip this if the mapView has not been set yet
Log.d(TAG, "moveCompassButton()");
// View view = mapView.findViewWithTag("GoogleMapCompass");
View view = mapView.findViewWithTag("GoogleMapMyLocationButton");
// move the compass button to the right side, centered
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.setMarginEnd(18);
view.setLayoutParams(layoutParams);
} catch (Exception ex) {
Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
ex.printStackTrace();
}
}
}
Non credo sia possibile. –
formato di codice migliore e fissato alcuni formati grammaticali – Robert